专栏名称: 51Testing软件测试网
51Testing软件测试网,人气最旺的软件测试技术门户,提供软件测试社区交流,软件测试博客,人才服务,测试沙龙,测试杂志,测试资料下载等全方位信息服务,是国内最专业的软件测试就业培训、企业服务供应商...
目录
相关文章推荐
51好读  ›  专栏  ›  51Testing软件测试网

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

51Testing软件测试网  · 公众号  · 测试  · 2017-04-01 17:30

正文


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

2 packageheadless_browser_testing;

3 import org.openqa.Selenium.WebDriver;

4 importorg.openqa.Selenium.htmlunit.HtmlUnitDriver;

5 import org.testng.Assert;

6 import org.testng.annotations.Test;

7

8 publicclassvefifyTestTitle {

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

14 publicvoidverifyFacebookTitle() {

15

16 //Call HtmlUnit Driver

17 WebDriver 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

21 driver.get("http://www.facebook.com");

22

23 //It will fetch the FB title and store in String

24 String facebook_Title= driver.getTitle();

25

26 //Assert condition will check the expected and actual title, if it matches

27 //our test passes

28 Assert.assertTrue(facebook_Title.contains("Facebook"));

29

30 System.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

2 packageheadless_browser_testing;

3 import java.io.File;

4 import org.openqa.Selenium.WebDriver;

5 import org.openqa.Selenium.phantomjs.PhantomJSDriver;

6 import org.testng.annotations.Test;

7

8 publicclass 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

13 publicvoidverifyFacebookTitle() {

14

15 //Set the path to access the phantomjs.exe file

16 File 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

18 System.setProperty("phantomjs.binary.path", src.getAbsolutePath());

19

20 //Call your PhantomJs Driver

21 WebDriver 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

25 driver.get("http://www.facebook.com");

26

27 //Print the currentURL of the page

28 System.out.println(driver.getCurrentUrl());

29 }

30 }

输出: https://www.facebook.com/

通过: 验证FacebookTitle

#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







请到「今天看啥」查看全文


推荐文章
历史其实很有趣  ·  胡适那些年的婚外情
8 年前