自动化测试,如何做到跨浏览器

使用自动化脚本在单个浏览器中测试网站是加速测试的简洁方法。只需单击一下,您就可以测试您的网站是否存在所有可能的错误,而无需手动点击和导航到网页。它是软件独创性的现代奇迹,可以节省数小时的手动时间并提高生产力。但是,要实现所有这些魔力,您需要首先构建自动化脚本。

在之前的文章中,我们专注于为运行selenium脚本设置 完整的测试套件环境。但是那个剧本有一个主要的缺点。该设置专注于仅在单个浏览器上进行测试。跨浏览器兼容性测试是所有测试人员的主要难点,也是所有功能测试周期的主要测试案例。

在这篇文章中,我们将重点关注同时在Chrome,Firefox和Opera浏览器上运行测试场景。我们将关注跨浏览器问题以及功能问题。

在开始之前,我强烈建议您阅读我们之前关于编写自动化测试脚本的文章。

安装Maven

第1步: 转到市场并安装Maven。

转到帮助→Eclipse Marketplace→搜索Maven→确认→确认→完成

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

重启Eclipse

步骤2: 重新启动Eclipse以使更改生效。重新启动Eclipse后,就可以开始创建项目了。

自动化测试,如何做到跨浏览器

创建一个Maven项目

步骤3: 要创建项目,请转到文件→新建→其他→Maven→项目。

您现在已经准备好创建一个Maven项目了。

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

在这里,您需要输入组ID和工件ID。假设组ID是 com.browsers1 ,工件ID是 crossbrowser。

输入ID后,单击Finish,将创建您的Maven项目。

自动化测试,如何做到跨浏览器

在左侧,您将找到两个文件夹,即 src/main/java 和 src/test/java。在 src/test/java,你会发现 com.browsers1.crossbrowser。右键单击 com.browsers1.crossbrowser,选择新建,然后创建一个类。

自动化测试,如何做到跨浏览器

自动化测试,如何做到跨浏览器

输入名称CrossbrowserTest,然后单击“完成”。

注意: 确保以大写字母开头,并以测试结束。

下载驱动程序

下一步是为浏览器安装驱动程序。由于您要使用自动化软件控制浏览器,因此需要确保要在脚本中使用的浏览器安装了驱动程序。

对于Firefox,您需要安装Geckodriver。对于Chrome,我们需要ChromeDriver。对于Opera,安装OperaChromiumDriver。由于我们将在脚本中使用这三种浏览器,因此我们需要安装这些浏览器。但是,如果您计划在脚本中添加更多浏览器,请确保已安装其驱动程序。

下载并安装驱动程序后,让我们从添加依赖项文件开始。必须为您正在使用的每个框架都有依赖项文件。所以我们需要在我们的pom.xml文件中下载Selenium的依赖文件。

选择pom.xml并删除to之间的行并使用以下命令添加依赖项文件:

org.testng 
testng 6.14.3 
test 
  
org.apache.maven.plugins maven-surefire-plugin 
2.19.1 
  
javax.mail mail 
1.5.0-b01 
org.seleniumhq.selenium 
selenium-htmlunit-driver 
2.52.0 
junit 
junit 
4.12 
info.cukes cucumber-java 
1.2.5 test info.cukes 
cucumber-picocontainer 1.2.5 test 
info.cukes cucumber-junit 1.2.5 test 
org.seleniumhq.selenium selenium-java 3.11.0 org.seleniumhq.selenium 

此代码将所有依赖项文件添加到项目中。

写下最终代码

现在保存它并创建一个脚本作为最后一步。

再次转到 src/test/java,选择crossbrowsertest.java文件,然后将代码复制到其工作区。

package com.browsers.Cross_Browser;
	import org.testng.annotations.Test;
 import org.testng.AssertJUnit;
	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.openqa.selenium.opera.OperaDriver;
	//comment the above line and uncomment below line to use Chrome
	//import org.openqa.selenium.chrome.ChromeDriver;
	public class BrowserTest {
	 WebDriver driver;
	 
	 @Test
	 public void AmazonTitleTest() {
	 // declaration and instantiation of objects/variables
	 System.setProperty("webdriver.gecko.driver","C:\\Users\\Admin\\Downloads\\geckodriver.exe");
	 driver = new FirefoxDriver();
	 //comment the above 2 lines and uncomment below 2 lines to use Chrome
	 //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
	 //WebDriver driver = new ChromeDriver();s
	 
	 String baseUrl = "https://www.amazon.com/";
	 String expectedTitle = "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more";
	 String actualTitle = "";
	 // launch Chrome and direct it to the Base URL
	 driver.get(baseUrl);
	 // get the actual value of the title
	 actualTitle = driver.getTitle();
	 /*
	 * compare the actual title of the page with the expected one and print
	 * the result as "Passed" or "Failed"
	 */
	 AssertJUnit.assertEquals(expectedTitle, actualTitle);
	 
	 //close Fire fox
	 driver.close();
	 
	 }
	 
	 @Test
	 public void AmazonTitleTest1() {
	 // declaration and instantiation of objects/variables
	 
	 //comment the above 2 lines and uncomment below 2 lines to use Chrome
	 System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");
	 WebDriver driver = new ChromeDriver();
	 
	 String baseUrl = "https://www.amazon.com/";
	 String expectedTitle = "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more";
	 String actualTitle = "";
	 // launch Chrome and direct it to the Base URL
	 driver.get(baseUrl);
	 // get the actual value of the title
	 actualTitle = driver.getTitle();
	 /*
	 * compare the actual title of the page with the expected one and print
	 * the result as "Passed" or "Failed"
	 */
	 AssertJUnit.assertEquals(expectedTitle, actualTitle);
	 
	 //close Fire fox
	 driver.close();
	 
	 }
	 
	 @Test
	 public void AmazonTitleTest2() {
	 // declaration and instantiation of objects/variables
	 
	 //comment the above 2 lines and uncomment below 2 lines to use Chrome
	 	System.setProperty("webdriver.ie.driver", "C:/Users/Admin/Downloads/IEDriverServer.exe");
			driver = new InternetExplorerDriver();
	 String baseUrl = "https://www.amazon.com/";
	 String expectedTitle = "Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more";
	 String actualTitle = "";
	 // launch Chrome and direct it to the Base URL
	 driver.get(baseUrl);
	 // get the actual value of the title
	 actualTitle = driver.getTitle();
	 /*
	 * compare the actual title of the page with the expected one and print
	 * the result as "Passed" or "Failed"
	 */
	 AssertJUnit.assertEquals(expectedTitle, actualTitle);
	 
	 //close Fire fox
	 driver.close();
	 
	 }
	
	}

粘贴此代码后,您现在需要将其转换为testng.xml。

要继续,请右键单击Crossbrowsertest.java,单击测试→转换为测试→下一步→单击复选框→完成。

现在将创建一个新文件testing.xml。

将testng.xml作为测试套件运行,您将全部执行自动跨浏览器测试。

此代码将在Chrome,Firefox和Opera中运行amazon.com,并测试网站是否打开。如果它打开,它将显示传递否则将显示失败。

您很快就会看到一个控制三个浏览器的自动化软件,您还会看到正在屏幕上执行的测试。

您还可以添加更多浏览器并更改要执行测试的URL。


分享到:


相關文章: