Saturday, March 28, 2020
Read properties file using ResourceBundle
package testngpractice;
import java.util.ResourceBundle;
import org.testng.annotations.Test;
public class PropertiesDemo {
@Test
public void showProp() {
ReadProperiesFile.getProperties("config", "name");
}
}
class ReadProperiesFile {
public static void getProperties(String filename, String key) {
ResourceBundle bundle = ResourceBundle.getBundle(filename);
System.out.println(bundle.getString(key));
}
}
import java.util.ResourceBundle;
import org.testng.annotations.Test;
public class PropertiesDemo {
@Test
public void showProp() {
ReadProperiesFile.getProperties("config", "name");
}
}
class ReadProperiesFile {
public static void getProperties(String filename, String key) {
ResourceBundle bundle = ResourceBundle.getBundle(filename);
System.out.println(bundle.getString(key));
}
}
Create a config.properties file in the Resource folder
Add Extent report to selenium TestNG and attache screenshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testngpractice; | |
import java.io.File; | |
import java.io.IOException; | |
import org.apache.commons.io.FileUtils; | |
import org.openqa.selenium.OutputType; | |
import org.openqa.selenium.TakesScreenshot; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import com.relevantcodes.extentreports.ExtentReports; | |
import com.relevantcodes.extentreports.ExtentTest; | |
import com.relevantcodes.extentreports.LogStatus; | |
public class BaseClassDemo { | |
static ExtentTest test; | |
static ExtentReports report; | |
@BeforeClass | |
public static void startTest() { | |
report = new ExtentReports(System.getProperty("user.dir") + "\\ExtentReportResults.html"); | |
test = report.startTest("ExtentDemo"); | |
} | |
@AfterClass | |
public static void endTest() { | |
report.endTest(test); | |
report.flush(); | |
} | |
public static String capture(WebDriver driver) throws IOException { | |
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); | |
File Dest = new File("src/../Images/" + System.currentTimeMillis() + ".png"); | |
String errflpath = Dest.getAbsolutePath(); | |
FileUtils.copyFile(scrFile, Dest); | |
return errflpath; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testngpractice; | |
import java.io.IOException; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.testng.annotations.Test; | |
import com.relevantcodes.extentreports.LogStatus; | |
public class ExtentTest extends BaseClassDemo { | |
@Test | |
public void extentReportsDemo() throws IOException { | |
System.setProperty("webdriver.chrome.driver","C:\\Users\\diya-mutturajh\\Desktop\\chromedriver\\chromedriver.exe"); | |
WebDriver driver = new ChromeDriver(); | |
driver.get("https://www.google.co.in"); | |
if (driver.getTitle().equals("Goog")) { | |
test.log(LogStatus.PASS, "Navigated to the specified URL"); | |
} else { | |
test.log(LogStatus.FAIL, "Test Failed"); | |
test.log(LogStatus.FAIL,test.addScreenCapture(capture(driver))+ "Test Failed"); | |
} | |
driver.close(); | |
driver.quit(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports --> | |
<dependency> | |
<groupId>com.relevantcodes</groupId> | |
<artifactId>extentreports</artifactId> | |
<version>2.41.2</version> | |
</dependency> |
Friday, March 20, 2020
[JAVA] Generate fake test data from faker library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.Locale; | |
import com.github.javafaker.Faker; | |
public class FackTestDataGenerator { | |
Faker faker; | |
FackTestDataGenerator() { | |
faker = new Faker(new Locale("en-IND")); | |
} | |
public String getFirstName() { | |
return faker.address().firstName(); | |
} | |
public static void main(String[] args) { | |
System.out.println(new FackTestDataGenerator().getFirstName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.github.javafaker</groupId> | |
<artifactId>javafaker</artifactId> | |
<version>1.0.1</version> | |
</dependency> |
Sunday, February 9, 2020
Read website and send mail in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import smtplib | |
URL = 'https://hulagabal.blogspot.com/' | |
page = requests.get(URL) | |
soup = BeautifulSoup(page.content, 'html.parser') | |
title = soup.find(id="header-inner").get_text() | |
print(title.strip()) | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login('email@gmail.com', '<your-password>') | |
subject = title.strip() | |
body = title.strip() | |
msg = f"Subject: {subject}\n\n{body}" | |
server.sendmail( | |
'Please check', | |
'to-email@gmail.com', | |
msg | |
) | |
print('Hey Email has been sent!') | |
server.quit() |
Subscribe to:
Posts (Atom)