• 使用TestNG进行多浏览器,跨浏览器和并行测试


    转自:https://blog.csdn.net/ProgrammerFan0101/article/details/83144355?utm_source=blogxgwz9

     

    当您的网站从模型转变为功能齐全的时候,您需要确保它对访问您网站的每个人都有效,无论他们使用的是Internet Explorer,Firefox还是其他任何浏览器。使用多种浏览器组合测试您的网站称为跨浏览器测试。

    您的网站在不同的浏览器中会有所不同。这是因为浏览器对某些代码的理解略有不同。您的设计师应该进行测试,以确保您的网站在所有现代浏览器中都能正常运行。但作为测试人员,我们需要确保至少在Internet Explorer,Firefox,Safari和Google Chrome浏览器上测试功能。

    使用Selenium TestNG进行多浏览器测试

    在每个项目中,都需要执行多浏览器测试,以确保每个浏览器的功能都按预期工作,以便为所有广泛的受众提供相同的用户体验。在每个浏览器上测试所有内容需要相当长的时间,当我们使用自动化来减少测试工作时,为什么我们不使用自动化执行多浏览器测试。TestNG为我们提供了在不同浏览器上以简单方便的方式执行相同测试的功能。

    怎么做…

    1)创建脚本以使用TestNG类测试LogIn应用程序。

    2)使用TestNG注释将“浏览器类型”作为参数传递给TestNG类的before方法。此方法将仅启动浏览器,该浏览器将作为参数提供。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    package automationFramework;

     

    import org.openqa.selenium.By;

     

    import org.openqa.selenium.WebDriver;

     

    import org.openqa.selenium.firefox.FirefoxDriver;

     

    import org.openqa.selenium.ie.InternetExplorerDriver;

     

    import org.testng.annotations.AfterClass;

     

    import org.testng.annotations.BeforeClass;

     

    import org.testng.annotations.Parameters;

     

    import org.testng.annotations.Test;

     

    public class MultiBrowser {

     

    public WebDriver driver;

     

      @Parameters("browser")

     

      @BeforeClass

     

      // Passing Browser parameter from TestNG xml

     

      public void beforeTest(String browser) {

     

      // If the browser is Firefox, then do this

     

      if(browser.equalsIgnoreCase("firefox")) {

     

      driver = new FirefoxDriver();

     

      // If browser is IE, then do this   

     

      }else if (browser.equalsIgnoreCase("ie")) {

     

      // Here I am setting up the path for my IEDriver

     

      System.setProperty("webdriver.ie.driver", "D:ToolsQAOnlineStoredriversIEDriverServer.exe");

     

      driver = new InternetExplorerDriver();

     

      }

     

      // Doesn't the browser type, lauch the Website

     

      driver.get("http://www.store.demoqa.com");

     

      }

     

      // Once Before method is completed, Test method will start

     

      @Test public void login() throws InterruptedException {

     

    driver.findElement(By.xpath(".//*[@id='account']/a")).click();

     

        driver.findElement(By.id("log")).sendKeys("testuser_1");

     

        driver.findElement(By.id("pwd")).sendKeys("Test@123");

     

        driver.findElement(By.id("login")).click();

     

    }  

     

      @AfterClass public void afterTest() {

     

    driver.quit();

     

    }

     

    }

     

    3) 创建TestNG XML以运行测试。配置TestNG XML以传递参数,即告诉应该使用哪个浏览器来运行测试。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    <?xml version="1.0" encoding="UTF-8"?>

     

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

     

    <suite name="Suite" parallel="none">

     

    <test name="FirefoxTest">

     

    <parameter name="browser" value="firefox" />

     

    <classes>

     

    <class name="automationFramework.MultiBrowser" />

     

    </classes>

     

    </test>

     

    <test name="IETest">

     

    <parameter name="browser" value="ie" />

     

    <classes>

     

    <class name="automationFramework.MultiBrowser" />

     

    </classes>

     

    </test>

     

    </suite>

     注意: 您可以在此处设置任意数量的浏览器,仅出于示例目的,我只设置了两个主浏览器。

    4)现在是时候运行xml了。右键单击testng.xml文件运行测试,然后选择Run As > TestNG Suite

    使用TestNG进行多浏览器,跨浏览器和并行测试

    注意:  TestNg将逐个执行测试。您可能希望执行并行测试,下一个主题将涵盖这一点。

     

    使用TestNG进行并行测试

    使用TestNG提供的并行执行功能。只需使用以上两个不同浏览器的登录应用程序示例。这次我们想要的是同时在两个浏览器中执行测试。

    现在只需在上面使用的xml 中将' parallel '属性设置为' tests ',然后再次运行。这次你会注意到你的两个浏览器几乎同时打开,你的测试将并行运行。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    <?xml version="1.0" encoding="UTF-8"?>

     

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

     

    <suite name="Suite" parallel="tests">

     

    <test name="FirefoxTest">

     

    <parameter name="browser" value="firefox" />

     

    <classes>

     

    <class name="automationFramework.MultiBrowser" />

     

    </classes>

     

    </test>

     

    <test name="IETest">

     

    <parameter name="browser" value="ie" />

     

    <classes>

     

    <class name="automationFramework.MultiBrowser" />

     

    </classes>

     

    </test>

     

    </suite>

  • 相关阅读:
    类和对象的一些BIF
    mac入门--通过hhomebrew下载过慢问题
    Vue的组件及传参
    Vue的指令和成员
    Vue基础(1)
    Python中好用的模块们
    Django-Auth认证模块
    Django杂篇(2)
    Django杂篇(1)
    Django的日常-AJAX
  • 原文地址:https://www.cnblogs.com/digod/p/13393355.html
Copyright © 2020-2023  润新知