Saturday, March 28, 2020

Read proprieties file in java


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));

}
}



Create a config.properties file in the Resource folder

Add Extent report to selenium TestNG and attache screenshots

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;
}
}
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();
}
}
view raw ExtentTest.java hosted with ❤ by GitHub
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
view raw pom.xml hosted with ❤ by GitHub

Friday, March 20, 2020

[JAVA] Generate fake test data from faker library

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());
}
}
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.1</version>
</dependency>
view raw pom.xml hosted with ❤ by GitHub