Selenium 如何与TestNG中进行跨浏览器测试

Selenium 如何与TestNG中进行跨浏览器测试

TestNG是一个强大的测试框架,是JUnit的增强版,TestNG在出现之前JUint一直被广泛使用。NG代表“下一代”。

一个网站应该在多个浏览器(如IE,Chrome,Firefox,Safari)上进行测试,以验证网站和功能的兼容性。由于HTML,CSS和JavaScript在所有浏览器中都是独特的,因此建议进行跨浏览器测试以确保网站的兼容性。Selenium支持跨浏览器测试,TestNG也支持。

本文将分析如何在Selenium与TestNG中执行跨浏览器测试。

步骤

  • 步骤1:确保Selenium、TestNG以及不同浏览器(如Firefox,IE,Chrome)的初始设置都已正确安装在系统中。
  • 步骤2:创建一个TestNG类,并根据程序代码中所述编写Selenium代码。
    • Parameter(’Browser’)将从testng.xml中读取值并传递给代码。
    • launchapp()将设置浏览器启动网站 http://www.calculator.net 的所有初始步骤。
    • CalculatePercent将执行实际执行和验证。
  • 步骤3:使用参数名称browser创建testng.xml文件,如testng.xml部分所示。
  • 步骤4:运行testng.xml文件。
  • 步骤5:它将同时在3个浏览器中运行测试用例。

示例

以下代码创建一个带有Selenium代码的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() {
      // Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();

      // Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();

      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");

      // Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");

      // Click Calculate Button
      driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click();

      // Get the Result Text based on its xpath
      String result =
         driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText();       

      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);

      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();
   }
}

testng.xml

这是一个用于组织和运行TestNG测试用例的配置文件。

当只需要执行有限测试而不是整套测试时,它非常方便。

示例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>">
<suite name="TestSuite" thread-count="3" parallel="tests" >
    <test name="ChromeTest">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="TestNGClass ">
            </class>
        </classes>
    </test>
    <test name="FirefoxTest">
        <parameter name="browser" value="firefox" />
        <classes>
            <class name="TestNGClass">
            </class>
        </classes>
    </test>
    <test name="ie">
        <parameter name="browser" value="ie" />
        <classes>
            <class name="TestNGClass">
            </class>
        </classes>
    </test>
</suite>

输出

[TestNG] Running:
  C://Users/**************
  Executing on CHROME
  Executing on IE
  Executing on FIREFOX  
  The Result is 5
  The Result is Pass
  The Result is 5
  The Result is Pass
  The Result is 5
  The Result is Pass


===============================================
Suite1
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

TestNG 精选笔记