自动化测试-Selenium WebDriver-运行多个测试用例

问题描述 投票:2回答:1

我在自动化测试中遇到了一些问题。我的Eclipse IDE中有大约50个测试用例。所有测试用例都在不同的类中。另外,我有一个包含@beforeclass和@afterclass的BaseClass。在@beforeclass中,浏览器打开,URL打开,网站URL打开,然后执行登录过程。然后我的测试用例就可以了。它们全部以@Test注释开头。我使用TestNG套件将它们连接起来。基类:My BaseClass.java classMyBaseClass.java classTestNg:My Suite测试用例示例(类):My Example Test Case (Class)

这是我的问题:我想对这些类(测试用例)使用优先级(例如@Test(priority = 1)),以减少劳动力。但是当我的代码有问题时;我的自动化测试停止了。我想,除了停下来还会继续。

第二个选项是使用TestNG。 TestNG可以,但是在每种情况下,浏览器都会打开。如何创建一个测试,例如只打开一个浏览器然后在该浏览器中运行所有测试用例?

顺便说一下,这是我的示例测试用例,您可以想象所有图片:

-@ beforeclass:打开浏览器-打开URL-登录

@@ test1:转到产品屏幕-单击创建产品-创建具有初始数量的产品-然后单击保存按钮,它应该再次出现在产品屏幕中。

@@ test2:再次单击创建产品按钮。 -创建没有初始数量的产品-单击“保存”按钮,它将出现错误。单击取消按钮以继续第三个@test

@ test3:等等...

-@ afterclass:关闭浏览器

非常感谢您的帮助!谢谢!


编辑:我像这样切换代码。因为我不想自动化,所以一遍又一遍地打开一个新的浏览器。我所有的测试用例仅涉及一个屏幕。 (产品)我想要的是通过订购:

  1. 启动@BeforeSuite(打开浏览器,URL ..)
  2. 启动@BeforeClass(转到“产品”屏幕-因为在所有情况下都是通用屏幕)
  3. 为测试用例1启动@Test
  4. 启动@AfterClass(再次转到产品屏幕以进行连接带有测试用例2
  5. 为测试用例2启动@Test

我的测试NG类:

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

<suite name="AllTests" verbose="10" >

  <test name="prcr1.1" >
    <classes>
       <class name="com.example.product.prcr1dot1" />
    </classes>
  </test>

  <test name="prcr1.2" >
    <classes>
       <class name="com.example.product.prcr1dot2" />
    </classes>
  </test>
  </suite>

我的基础班:

public class BaseClass {
    protected WebDriver driver;

    @BeforeSuite

    public void openMyexample() throws InterruptedException {
        System.out.println("Initiate LoginTest Test...");
        driver = utilities.DriverFactory.open("chrome");
        driver.manage().window().maximize();
        driver.get("https://mystage.example.com/en/public/login/?isTestAutomationLive=true");
        System.out.println("example Page Has Been Opened..");

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.elementToBeClickable((By.name("email"))));

        // Enter Username Section
        WebElement username = driver.findElement(By.name("email"));
        username.sendKeys("[email protected]");
        Thread.sleep(1000);
        wait.until(ExpectedConditions.elementToBeClickable((By.name("password"))));
        // Enter Password Section
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("Test*01");
        Thread.sleep(1000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//form[@id='frmLogin']/button"))));

        // Click Login Button
        WebElement logInButton = driver.findElement(By.xpath("//form[@id='frmLogin']/button"));
        logInButton.click();

        // PAGE LOADER

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//span[contains(.,'×')]"))));

        Thread.sleep(1000);

        // Integration Message Function WebElement - Option 1: WebElement
        WebElement popupCancelButton = driver.findElement(By.xpath("//span[contains(.,'×')]"));
        popupCancelButton.click();
        Thread.sleep(3000);
    }

    @BeforeClass
    public void openProducts() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);
        // From Dashboard Section to Product Section
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
        WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
        productButton.click();
        Thread.sleep(4000);

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));
        WebElement createProductButton = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
        createProductButton.click();
        Thread.sleep(4000);
    }

    @AfterClass
    public void closeProducts() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
        WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
        productButton.click();
        Thread.sleep(4000);

    }

    @AfterSuite
    public void closeMyexample() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, 60);

        Thread.sleep(4000);

        // Sign Out Thread.sleep(5000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//div[2]/i"))));
        WebElement logoutScrollDownButton = driver.findElement(By.xpath("//div[2]/i"));
        logoutScrollDownButton.click();
        Thread.sleep(3000);
        WebElement signoutButton = driver.findElement(By.xpath("//a[contains(.,'Sign Out')]"));
        signoutButton.click();

        // Close Browser
        System.out.println("Your Test Has Been Ended Successfully.");
        Thread.sleep(1000);
        System.out.println("Your Test is going to Close..");
        driver.quit();

    }

    // Sleep Function
    private void sleep(long m) {
        try {
            Thread.sleep(m);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我的测试案例1:

package com.example.product;

import utilities.BaseClass;

//Test Case 1: PRCR - 1.1
//Creating a new product with a unique SKU 
public class prcr1dot1 extends BaseClass {
    @Test
    public void prcr1dot1() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        String uuid = UUID.randomUUID().toString();
        driver.findElement(By.name("sku")).sendKeys(uuid);

        driver.findElement(By.name("name")).sendKeys(uuid);

        WebElement packType = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType.click();

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'SAVE')]"))));
        WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
        productSaveButton.click();
        Thread.sleep(8000);
    }
}

我的测试用例2:

import utilities.BaseClass;

public class prcr1dot2 extends BaseClass {
    // Test Case 2: PRCR - 1.2
    // Creating a new product with the used SKU

    @Test
    public void prcr1dot2() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        driver.findElement(By.name("sku")).sendKeys("SKU08");

        String uuid = UUID.randomUUID().toString();
        driver.findElement(By.name("name")).sendKeys(uuid);

        WebElement packType = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType.click();

        JavascriptExecutor jsx = (JavascriptExecutor) driver;
        jsx.executeScript("window.scrollBy(0,450)", "");

        WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
        productSaveButton.click();

        Thread.sleep(5000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));

        WebElement createProductButton2 = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
        createProductButton2.click();

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        driver.findElement(By.name("sku")).sendKeys("SKU08");

        String uuid2 = UUID.randomUUID().toString();
        driver.findElement(By.name("name")).sendKeys(uuid2);

        WebElement packType2 = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType2.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        String uuid3 = UUID.randomUUID().toString();
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid3);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType2 = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType2.click();

        /*
         * WebElement productSaveButton2 =
         * driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
         * productSaveButton2.click(); Thread.sleep(5000); WebElement
         * productCancelButton2 =
         * driver.findElement(By.xpath("//button[contains(.,'CANCEL')]"));
         * productCancelButton2.click(); Thread.sleep(2000);
         * wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
         * "//button[contains(.,'YES')]")))); WebElement productCancelYesButton =
         * driver.findElement(By.xpath("//button[contains(.,'YES')]"));
         * productCancelYesButton.click();
         */

    }

}
selenium testing automation testng software-quality
1个回答
0
投票

我不确定它能否解决您的问题。但是这个想法对我有用。使用这种方法,您可以在同一浏览器上运行所有测试用例,而无需多次打开。

这里有三堂课

test1.java

test2.java

test2.java

Mainclass.java

这里是test1.java

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class test1 {

    @BeforeClass
    public void before(){

    }
    @Test
    public static void test(WebDriver driver){
        driver.get("https://www.google.com");
    }
    @AfterMethod
    public void after(){

    }
}

这里是test2.java

package test;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class test2 {
    @Test
    public static void test(WebDriver driver){
        driver.get("https://www.yahoo.com");
    }
}

这里是mainclass.java文件,我在其中将驱动程序会话传递给了测试

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Mainclass {

    public static void main(String[]args){
        WebDriver driver;
        driver=new ChromeDriver();
        test1.test(driver);
        test2.test(driver);
    }

}

现在您需要从testing.xml文件运行mainclass。

在主类中,您可以一次打开浏览器,并将驱动程序传递给所有测试用例,以便它将使用浏览器窗口来运行测试用例。

我不确定是否有其他理想的主意可用,但肯定会为您服务

© www.soinside.com 2019 - 2024. All rights reserved.