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

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

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

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

  Selenium只支持基于Web的应用程序,要想打开它们,我们需要一个浏览器,但Selenium可以支持各种浏览器进行测试自动化。这篇文章中,我们将阐释如何在市场上出现的各种不同浏览器中设置驱动程序。
  在当前的大环境下,有三种主流的浏览器被广泛使用进行测试,即Google Chrome,Mozilla Firefox和Internet Explorer。但是,Selenium同样也支持其他的浏览器。要想在不同浏览器上执行脚本,我们需要该浏览器的驱动程序。
  当我们执行Selenium自动化时,我们的第一行代码应该是:
  WebDriver driver = new FireFoxDriver ();
  这意味着WebDriver是一个接口,我们正在定义一个类型为接口的引用变量(驱动程序)。现在,我们分配给它的任何对象都必须是类(FireFoxDriver)的实例或者实现该接口的任何其他驱动程序。在我们的例子中,FireFoxDriver是一个类,接口是WebDriver。
  当所有的驱动程序设置完成后,我们开始执行任何Selenium命令,如:
  driver.getTitle ();
  如下图所示:
  在内部创建一个HTTP请求并将其发送到定义的特定浏览器驱动程序,浏览器驱动程序使用该HTTP服务器获取HTTP请求,并确定实现Selenium命令所需步骤。我们创建逻辑在浏览器上执行,然后将执行结果发送回HTTP服务器,并再次发回自动化脚本。因此,在设置驱动程序后,我们可以访问驱动程序类的所有内置方法,如:
  · findElement();
  · close();
  · getClass(); and many more
  如下图所示:
  可以输入"driver"通过这些方法。在编辑器中将显示所有的方法,或者你可以按"ctrl+space",也可显示。
  如下图所示:
  有时候你按下"ctrl+space"会显示内置方法无法访问。那么你需要检查在"环境变量"中进行的JAVA_HOME路径设置,并确保它们是正确的。
  设置环境变量的步骤:
  · 转到控制面板 - >单击系统
  · 转到高级系统设置
  · 单击"环境变量"按钮
  · 点击新建按钮设置JAVA_HOME路径
  Selenium自带默认Mozilla Firefox驱动程序,该驱动程序在Selenium WebDriver jar文件中。这就是为什么调用Firefox驱动程序不需要安装。如果我们要使用其他浏览器,我们需要设置其系统属性。
  现在,我们将在下面的提到的浏览器中设置和执行驱动器:
  1) Mozilla Firefox
  2) Google Chrome
  3) Internet Explorer
  4) Opera
  5) Ghost Driver or PhantomJS
  6) HTML Unit
  假设你们都知道上面提到的不同的浏览器,那么接下来将解释什么是Ghost驱动程序和HTML单元驱动程序的功能以及如何设置脚本。
  #1)HTML单元驱动程序:
  使用此驱动程序,我们可以执行Headless Browser Testing,这也就意味着在内部运行时没有GUI,并且,你无法像在普通浏览器中执行所有操作。对于使用HTML单元驱动程序,不需要安装任何其他API或jar文件。一旦你有Selenium服务器独立的jar文件,你便可以使用它。
  可以参考以下代码:
    
1//Create a Java Project, under it create a package, and under package create a class
2packageheadless_browser_testing;
3import org.openqa.Selenium.WebDriver;
4importorg.openqa.Selenium.htmlunit.HtmlUnitDriver;
5import org.testng.Assert;
6import org.testng.annotations.Test;
7
8publicclassvefifyTestTitle {
9
10//You can run your script with TestNG or JUnit or using Java Application
11// I am using TestNG and using TestNG annotations
12
13@Test
14publicvoidverifyFacebookTitle() {
15
16//Call HtmlUnit Driver
17WebDriver driver = newHtmlUnitDriver(true);
18
19//It will get the Facebook URL and run the script in background, means you
20//will not see the Facebook page
21driver.get("http://www.facebook.com");
22
23//It will fetch the FB title and store in String
24String facebook_Title= driver.getTitle();
25
26//Assert condition will check the expected and actual title, if it matches
27//our test passes
28Assert.assertTrue(facebook_Title.contains("Facebook"));
29
30System.out.println(facebook_Title);
31}
32}
  输出:Facebook 登录或注册
  通过:验证FacebookTitle
  不建议将HTML单元驱动程序用于复杂的应用程序之中,在默认情况下不支持java脚本,因此必须给予条件的真实支持
  #2) PhantomJS 驱动程序:
  PhantomJS浏览器也用于执行Headless Browser Testing,它使用的是JavaScript AP。你可以用它进行Headless Website 测试和网页访问。HTML单元驱动程序的一个优点是可以用于捕获截图,这也就意味着测试能够在后台运行并捕获截图。
  为了使用PhantomJS浏览器与Selenium WebDriver,我们必须下载安装GhostDriver。它是用于PhantomJS浏览器的简单JS中的WebDriver线路协议的实现。在PhantomJS的最新版本中,他们将GhostDriver与PhantomJS结合在了一起。因此,我们现在不需要单独安装。执行PhantomJS,我们需要PhantomJS驱动器。同时,在运行这个脚本时,我们需要设置PhantomJs.binary.路径。
  参考以下代码:
1//Create a Java Project, then under it create a package, under package create a class
2packageheadless_browser_testing;
3import java.io.File;
4import org.openqa.Selenium.WebDriver;
5import org.openqa.Selenium.phantomjs.PhantomJSDriver;
6import org.testng.annotations.Test;
7
8publicclass phantom_Js_Driver {
9//You can run your script with TestNG or JUnit or using Java Application
10// I am using TestNG and using TestNG annotations
11
12@Test
13publicvoidverifyFacebookTitle() {
14
15//Set the path to access the phantomjs.exe file
16File src = newFile("E:\\exe\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
17//You need to specify the property here and give path of driver
18System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
19
20//Call your PhantomJs Driver
21WebDriver driver = newPhantomJSDriver();
22
23//It will get the Facebook URL and run the script in background, means you
24//will not see the Facebook page
25driver.get("http://www.facebook.com");
26
27//Print the currentURL of the page
28System.out.println(driver.getCurrentUrl());
29 }
30}
  输出:https://www.facebook.com/
  通过:验证FacebookTitle
版权声明:51Testing软件测试网(www.51testing.com)原创出品,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号