如何在不同浏览器中运行Selenium WebDriver?

发表于:2017-4-01 09:17

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:selenium官网    来源:51Testing软件测试网原创

  #3) Mozilla Firefox 驱动器:
  如何在Firefox浏览器中运行WebDriver:
  对于调用Firefox驱动器,无需安装或者建立额外的jar文件,它是Selenium WebDriver支持的默认的驱动程序。
  请参考以下执行代码:
1 package Different_Drivers;
2
3 import org.openqa.Selenium.WebDriver;
4 import org.openqa.Selenium.firefox.FirefoxDriver;
5 import org.testng.annotations.Test;
6 import org.testng.Assert;
7
8 public class FF_Driver {
9
10 @Test
11 public void Test_Gmail_Login() {
12 WebDriver driver = new FirefoxDriver();
13 driver.get("http://www.gmail.com");
14 driver.findElement(By.id("Email")).sendKeys("Enter user name");
15 driver.findElement(By.id("next")).click();
16 Thread.sleep(2000);
17 driver.findElement(By.id("Passwd")).sendKeys("Enter Password");
18 driver.findElement(By.id("signIn")).click();
19 Thread.sleep(2000);
20 String title_Of_Page = driver.getTitle();
21 Assert.assertEquals(driver.getTitle(), title_Of_Page);
22 System.out.println("Page title matched");
23 }
24 }
  输出: 页面匹配
  通过: Test_Gmail_Login
  #4) Google Chrome驱动器:
  如何在Chrome浏览器中运行WebDriver:
  要调用Google Chrome驱动程序,首先应该下载驱动,然后接着使用以下代码设置系统属性:
1  packageDifferent_Drivers;
2  importorg.openqa.Selenium.WebDriver;
3  importorg.openqa.Selenium.chrome.ChromeDriver;
4  importorg.testng.annotations.Test;
5  importjava.util.Iterator;
6  importjava.util.Set;
7
8  publicclassgoogleChrome_Driver{
10  @Test
11  publicvoidTest_Rediff_Alert()throwsInterruptedException{
12
13  //setsystemproperty,sothatwecanaccesschromedriver
14  System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
15
16  //ItwillopentheChromebrowserandexecuteyourlogic
17  WebDriverdriver=newChromeDriver();
18
19  //Openrediffpageinchromebrowser
20  driver.get("http://www.rediffmail.com");
21
22  //waitforpagetoload
23  Thread.sleep(5000);
24  //Itwillgetandstorethemainwindowpagehandleorid
25  Stringmainpage=driver.getWindowHandle();
26  StringsubwinhandleString=null;
27  //setaloopwhichwillstoreallwindowpopuphandles
28  Set<String>handle=driver.getWindowHandles();
29  Iterator<String>iterator=handle.iterator();
30  while(iterator.hasNext()){
31  subwinhandleString=iterator.next();
32  }
33  driver.switchTo().window(subwinhandleString);
34  System.out.println(driver.getTitle());
35  Thread.sleep(2000);
36  driver.close();
37
38  //Againswitchbacktomainwindow
39  driver.switchTo().window(mainpage);
40  System.out.println(driver.getTitle());
41  }
42  }
Output:Welcometorediff.com
Rediff.com:OnlineShopping,Rediffmail,LatestIndiaNews,Business,Bollywood,Sports,Stock,LiveCricketScore,Money,MovieReviews
PASSED:Test_Rediff_Alert
  输出:欢迎来到rediff.com
  Rediff.com:OnlineShopping,Rediffmail,LatestIndiaNews,Business,Bollywood,Sports,Stock,LiveCricketScore,Money,MovieReviews
  通过:Test_Rediff_Alert
  #5)Internet Explorer驱动程序:
  如何在IE浏览器中运行WebDriver:
  要调用Internet Explorer驱动程序,请下载驱动程序并设置其系统属性
  参考以下代码:
1 package Different_Drivers;
2
3 import org.openqa.selenium.WebDriver;
4 import org.openqa.selenium.ie.InternetExplorerDriver;
5 import org.openqa.selenium.remote.DesiredCapabilities;
6 import org.testng.annotations.Test;
7
8 public class&amp;nbsp; internetExplorer_Driver {
9
10 @Test
11 public void ieDriver() throws InterruptedException {
12
13 //set system property, so that we can access IE driver
14 System.setProperty("webdriver.ie.driver","E\\IEDriverServer.exe");
15
16 //set desiredcapabilites for calling ie driver
17 DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
18 capabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
19 WebDriver driver = new InternetExplorerDriver(capabilities);
20
21 driver.get("https://www.google.com");
22
23 Thread.sleep(5000);
24
25 String title_Of_Page = driver.getTitle();
26
27 System.out.println(title_Of_Page);
28 }
29 }
  输出: Google
  通过: ieDriver
  #6) Opera驱动程序:
  使用Opera驱动程序,先下载驱动器并设置系统属性
  参考以下代码:
1 package Different_Drivers;
2 import org.openqa.Selenium.WebDriver;
3 importorg.openqa.Selenium.opera.OperaDriver;
4 import org.testng.annotations.Test;
5 import org.testng.Assert;
6
7 public class operaDriver {
8  @Test
9 public void createAccount(){
10
11 //set system property, so that we can access opera driver
12 System.setProperty("webdriver.opera.driver", "E:\\operadriver.exe");
13
14 // it will open the opera browser
15 WebDriver driver = newOperaDriver();
16 driver.get("https://www.google.com/intl/en/mail/help/about.html");
17
18  // Here driver will try to find out create an account link on the application
19 WebElement createAccount = driver.findElement(By.xpath(".//*[@id='gmail-create-account']"));
20 Assert.assertTrue(createAccount.isDisplayed());
21 //Create Account will be clicked only if the above condition is true
22 createAccount.click();
23 System.out.println(createAccount.getText());
24  }
25 }
Output: Create an account
PASSED: operaDriver
  输出: 创建一个账户
  通过: operaDriver
  在这篇文章中,介绍了如何设置和使用各种浏览器的驱动程序。
  我们了解了HTML单元驱动程序和PhantomJS驱动程序与其他常用浏览器不同。关于这一点的原因之一是因为它不用于常用浏览,像类似于Google Chrome这种。相反,因为不支持GUI,它们在内部运作并且能够很快执行我们的命令。现在我们了解到了如何设置驱动程序,并且知道了如何在各种浏览器上运行它们,那么现在我们需要做的就是明确为什么要这么做。众所周知,Selenium仅支持基于Web的应用程序,并且要打开它们就需要一个浏览器。现在有了各种的驱动器(上边已经提及到了)可用于打开这些浏览器,WebDriver是一个包含定义的所有抽象方法的接口,因此,这也是我们这篇文章所讨论的问题价值所在。
版权声明:51Testing软件测试网(www.51testing.com)原创出品,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号