【问题标题】:Unable to run multiple test cases from multiple classes in a single instance of browser in TestNG无法在 TestNG 的单个浏览器实例中运行来自多个类的多个测试用例
【发布时间】:2023-01-28 21:05:05
【问题描述】:

我正在尝试运行放置在不同类中的多个测试用例,但是当我运行代码时,最后一个类创建了一个新的 chrome 驱动程序实例。这段代码与公司的工作有关,因此我无法详细分享完整的代码,但我会尽可能多地分享,以帮助您更好地理解。

这是代码:

主要类别:

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

public class Main1 {
    
    public static WebDriver driver;
        public static WebDriverWait w;
    
    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Person\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

}

二等舱:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Second extends Main{
    
    @BeforeClass
    public void openWebsite() {
        driver.get("http://www.example.com");
        System.out.println("Verfying Title...");
        String expTitle = "Company Title";
        String actualTitle = driver.getTitle();
        if (expTitle.equals(actualTitle)) {
            System.out.println("Title Verified");
        } else {
            System.out.println("Title Not Verified");
        }
    }
    
    @Test(priority=1)
    public void companyCode() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(30));

        //Dropdown to select company

    }
    
    @Test(priority=2)
    public void reload() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(2));
        
        
        //Click another tab on the application and click reload
    
    @Test(priority=3)
    public void orgtree() {

        By by = By.xpath("//span[normalize-space()='node']//preceding-sibling::div[2]");
        retryingFindClick(driver, by);  //Click Expand node

        //clicking on elements on the website
        
    }
    
    public boolean retryingFindClick(WebDriver driver, By by) {
        boolean result = false;
        int attempts = 0;
        while(attempts < 5) {
            try {
                driver.findElement(by).click();
                result = true;
                break;
            } catch(Exception e) {
            }
            attempts++;
        }
        return result;
    }

}

最后一课:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;

public class Last extends Main {
    
    @Test
    public void Window1() {
        
        w = new WebDriverWait(driver, Duration.ofSeconds(2));
        
        //Click on a window on the website
        
        
    }

}

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">


<suite name="TestSuite1">
  <test thread-count="5" name="Test1">
    
    <classes>
        <class name="test.Main"/>
        <class name="test.Second"/>
        <class name="test.Last"/>
    </classes>
    
    
  </test> <!-- Test1 -->
</suite> <!-- TestSuite1 -->

现在的问题是,直到 Second Class 之前,执行都是在浏览器的单个实例中进行的,但是当执行 Last Class 时,会创建一个新的浏览器实例,然后从头开始。我希望我在 xml 文件中添加的任何类的所有测试用例都在一个实例中运行。

在此之前我尝试过许多其他方法,但每次都会出现错误,例如“无法实例化第二类”或调用错误等。这是我尝试过的最好的方法,我没有收到任何错误,但我不知道为什么 Last Class 在按照 xml 文件中的指示在 Second Class 执行后立即运行时创建一个新实例

【问题讨论】:

    标签: testng testng-dataprovider testng-eclipse testng.xml testng-annotation-test


    【解决方案1】:

    在主类中尝试 @BeforeTest 而不是 @BeforeClass

    还使用 testng.xml 中的 preserve-order = true 按顺序运行类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      相关资源
      最近更新 更多