1. Switch Browser class: It contains a way to switch browsers, as well as close the browser, set the wait time, and override the assertion method
Package com.rrx.framework;
Import java.io.IOException;
Import Java.util.concurrent.TimeUnit;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.chrome.ChromeDriver;
Import Org.openqa.selenium.firefox.FirefoxDriver;
Import Org.openqa.selenium.ie.InternetExplorerDriver;
Import Org.testng.Assert;
Import Org.testng.Reporter;
Import Org.testng.annotations.Parameters;
public class Borwserengin {
Public String Browsername;
Public String URL;
public Webdriver driver;
Public void Initconfigdate () throws IOException {
browsername = propertiesengine.getproperties ("Browsername");
url = propertiesengine.getproperties ("url");
//system.out.println (browsername);
}
@Parameters ("Browser")
Public Webdriver Getdriver () throws IOException {
initconfigdate ();
Logger.getlogger (). info ("browser name" +browsername);
Logger.getlogger (). info ("URL:" +url);
System.out.println (browsername);
if (browsername.equalsignorecase ("Firefox")) {
system.setproperty ("Webdriver.firefox.bin", "" ");
Driver = new Firefoxdriver ();
} else if (Browsername.equalsignorecase ("Chrome")) {
system.setproperty ("Webdriver.chrome.driver",
"C:\\users\\administrator\\workspace\\seleniumkuangjia\\driver\\chromedriver.exe");
Driver = new Chromedriver ();
} else if (Browsername.equalsignorecase ("IE")) {
system.setproperty ("Webdriver.ie.driver", "" ");
Driver = new Internetexplorerdriver ();
}
Driver.get (URL);
return driver;
}
/**
*//Close browser and launch
*/
Public void TearDown () {
driver.quit ();
}
/**
* Implicit time-wait method
* Imlicitlywait is an implicit wait, usually used when looking for an element. For example, I set a Find element to a maximum time of 10 seconds, using the
after imlicitlywait, if the element is not found for the first time, the element will be recycled within 10 seconds, knowing that more than 10 seconds, the report timeout error.
*/
Public void Calltime (int time) {
driver.manage (). Timeouts (). Implicitlywait (time, timeunit.seconds);
}
/**
* Assertion Method
*/
Public static void Assertequal (Object object,object object2) {
try {
Assert.assertequals (object, Object2, "not the Same");
} catch (Error e) {
Reporter.log ("" +e);//Assertion error is to print the log in the test report
Logger.getlogger (). info (e);//print in Logger log
}
}
}
2. The output of the log uses LONG4J and reporter two methods. Reporter will print the logs to the test report. If you don't know the exact method, you can view my previous blog.
JAVA+SELENIUM+TESTNG Building Automation Test Architecture (2) cross-browser capabilities