【问题标题】:Automation Testing - Selenium WebDriver - Running Multiple Test Cases自动化测试 - Selenium WebDriver - 运行多个测试用例
【发布时间】:2020-02-29 12:59:14
【问题描述】:

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

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

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

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

-@beforeclass:打开浏览器-打开网址-登录

@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("automationproducts4@example.com");
        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:

包 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();
         */

    }

}

【问题讨论】:

  • 你能告诉我们你写了什么吗?如果可能,sn-p 代码?
  • 我的建议:尝试将所有测试用例完全分开。例如:如果在您的测试用例中,您需要登录,请尝试检查您是否已登录,然后继续。如果您没有登录,请登录。在这种情况下,如果发生任何错误,您只需关闭浏览器并重新启动它,而不会影响您的其他测试用例。
  • 根据我的经验,把测试用例做成链条会带来很多困难,而且随着测试用例数量的增加,测试用例的管理难度会越来越大。
  • @ManojKengudelu 我添加了所有信息!谢谢!
  • @M3trix 谢谢你的建议,但我做不到。所有测试用例都相互关联,我要做的就是,做测试用例 1,再次进入产品屏幕并做测试用例 2

标签: selenium testing automation testng software-quality


【解决方案1】:

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

我这里有三节课

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。

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

我不确定是否有其他理想的想法,但它肯定对你有用

【讨论】:

  • 非常感谢您的帮助。但我觉得我的情况有点不同。所有这些测试用例相互依赖。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2013-04-11
  • 2015-10-30
  • 2013-02-20
  • 1970-01-01
  • 2016-04-17
  • 2021-05-15
相关资源
最近更新 更多