JAVA+SELENIUM+TESTNG Building Automation Test Architecture (2) cross-browser capabilities

Source: Internet
Author: User
Tags assert object object testng

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.