java+Selenium+TestNg搭建自动化测试架构实现跨浏览器功能

Posted 小李卫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java+Selenium+TestNg搭建自动化测试架构实现跨浏览器功能相关的知识,希望对你有一定的参考价值。

1.切换浏览器类:其中包含了切换浏览器的方法,以及关闭浏览器,设置等待时间,以及重写的断言方法

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("浏览器名称"+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;
}

/**
* //关闭浏览器并且推出
*/
public void tearDown() {
driver.quit();
}
/**
* 隐式时间等待方法
* imlicitlyWait是隐式等待,一般在查找元素的时候使用。例如,我设置一个查找元素最大时间为10秒,使用了
imlicitlyWait后,如果第一次没有找到元素,会在10秒之内不断循环去找元素,知道超过10秒,报超时错误。
*/
public void callTime(int time){

driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
}

/**
* 断言方法
*/
public static void assertEqual(Object object,Object object2){
try {
Assert.assertEquals(object, object2,"不相同");
} catch (Error e) {

Reporter.log(""+e);//断言错误是把日志打印在测试报告中
Logger.getLogger().info(e);//打印在logger日志中

}

}
}

2.日志的输出采用long4j和Reporter两种方法。Reporter会将日志打印到测试报告中。如果不知道具体方法的可以查看我之前的博客。

 

































































以上是关于java+Selenium+TestNg搭建自动化测试架构实现跨浏览器功能的主要内容,如果未能解决你的问题,请参考以下文章

java+selenium+testng+reportng+jenkins自动化环境搭建

selenium+java+testNG+maven环境搭建

java+Selenium+TestNg搭建自动化测试架构实现POM(page+Object+modal)

java+Selenium+TestNg搭建自动化测试架构实现POM(page+Object+modal)

自动化测试历程 selenium+testng+java+reportng+jenkins

Mac系统搭建java+selenium+testng环境