求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 

Selenium教程
Selenium概述
Selenium IDE
Selenium IDE下载
Selenium IDE 工具特点
Selenium IDE测试创建
Selenium IDE测试
Selenium IDE验证点
Selenium - IDE模式匹配
Selenium用户扩展
Selenium IDE- 不同的浏览器
Selenium 环境安装设置
Selenium RC
Selenium - Selenese命令
Selenium Webdriver
Selenium定位器
文本框的相互作用
单选按钮互动
复选框交互
下拉框交互
Synchronization 同步
拖放
键盘操作
鼠标操作
多选择操作
查找所有链接
Selenium测试设计技术
Selenium页面对象模型
使用Excel数据驱动
log4j日志
异常处理
多浏览器测试
捕捉屏幕截图
捕捉视频
Selenium TestNG
Selenium网格
 
 

多浏览器测试
1058 次浏览
2次  

多浏览器测试

用户可以同时执行多个浏览器中的脚本。为了演示,我们将充分利用我们已经采取了Selenium 网格相同的场景。Selenium 网格的例子,我们已经在远程执行脚本,在这里将在本地执行脚本。

即使对于这一点,我们必须确保我们有适当的驱动程序下载。请参考Selenium 网格章下载IE和Chrome浏览器的驱动程序。

示例

我们将在所有浏览器中同时执行%的计算用于演示目的。

package TestNG;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.testng.annotations.*;

public class TestNGClass
{
private WebDriver driver;
private String URL = "http://www.calculator.net";

@Parameters("browser")
@BeforeTest
public void launchapp(String browser)
{

if (browser.equalsIgnoreCase("firefox"))
{
System.out.println(" Executing on FireFox");
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.out.println(" Executing on CHROME");
System.out.println("Executing on IE");
System.setProperty("webdriver.chrome.driver", "D:chromedriver.exe");
driver = new ChromeDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("ie"))
{
System.out.println("Executing on IE");
System.setProperty("webdriver.ie.driver", "D:IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
else
{
throw new IllegalArgumentException("The Browser Type is Undefined");
}
}


@Test
public void calculatepercent()
{
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Click on Percent Calculators
driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50"); // Enter value 50 in the second number of the percent Calculator
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Click Calculate Button
String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Get the Result Text based on its xpath
System.out.println(" The Result is " + result); //Print a Log In message to the screen

if(result.equals("5"))
{
System.out.println(" The Result is Pass");
}
else
{
System.out.println(" The Result is Fail");
}
}

@AfterTest
public void closeBrowser()
{
driver.close();
}
}

创建一个XML这将有助于我们在参数设置浏览器的名字,不要忘记提及 parallel="tests"为了同时在所有浏览器中执行。

通过对XML文件进行右键点击执行脚本,然后选择 'Run As' >> 'TestNG' 方式,如下图所示。

输出

所有的浏览器将平行展开,结果将被打印在控制台上。

注:对于我们在IE浏览器执行成功确保复选框“启用保护模式”下的“IE选项中的安全选项卡中选中或未在所有区域中未检查。

TestNG的结果以HTML格式来查看详细的分析。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1058 次浏览
2次
 捐助