gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:13:01 26 4
gpt4 key购买 nike

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

}

}

最佳答案

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

这里我有三个类

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

这是我将驱动程序 session 传递给测试的 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 文件运行主类。

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

我不确定是否还有其他理想的想法,但它肯定适合你

关于selenium - 自动化测试 - Selenium WebDriver - 运行多个测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com