使用 Java 和 TestNG 创建测试套件的奇怪问题

问题描述 投票:0回答:0

我现在有很多测试,是我在过去几周为我们公司构建的,但现在我想创建一个测试套件,这样我就可以并行运行测试,但我遇到了一些奇怪的问题。

当我在主类中正常运行它们时,或者如果我将

thread-count="1"
放入我的 xml 中以便它们逐一运行,但现在当我尝试并行运行它们时,我的每个测试都工作得很好。

发生的事情是,例如,在我的每个测试中,我首先登录我们的网站。如果在我的 xml 文件中插入行

thread-count="3"
第一个打开的浏览器尝试登录 3 次,所以它基本上输入邮件、密码并单击登录按钮 3 次,而其他 2 个浏览器只是空白。

完成前 3 个浏览器后,下一个浏览器打开并且工作正常。他们登录但然后他们无缘无故地死了?有时他们会多走几步,但最终他们都会无缘无故地死去,而且他们永远不会完成。

我真的很感激你们能给我的任何帮助/提示,我现在真的很迷茫。

如果还有更好的代码方法,请告诉我。

我的代码:

Main.java

public class Main {

public WebDriver driver;
//String downloadFilepath = "/path/to/download";
String path = System.getProperty("user.dir"); 
String fullpath = path + "\\dokumenti";

@BeforeMethod
public void setUp() {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\nsapac\\driver\\111\\drajver\\chromedriver.exe");
    
    Map<String, Object> preferences = new Hashtable<String, Object>();
    preferences.put("profile.default_content_settings.popups", 0);
    preferences.put("download.prompt_for_download", "false");
    preferences.put("download.default_directory", fullpath);
    
    // disable flash and the PDF viewer
    preferences.put("plugins.plugins_disabled", new String[]{
        "Adobe Flash Player", "Chrome PDF Viewer"});
    
    ChromeOptions options = new ChromeOptions();
    
    options.setExperimentalOption("prefs", preferences);
    

    options.addArguments("--start-maximized");
    options.addArguments("--remote-allow-origins=*");
    
    driver = new ChromeDriver(options);
    
}


@Test
public void PotovanjeTest() throws InterruptedException, AWTException, IOException {
    
    Potovanje po = new Potovanje(driver);
    po.PotovanjeTest();
}

@Test
public void AvtoTest() throws InterruptedException, AWTException, IOException {
    
    
    Avtomobili avto = new Avtomobili(driver);
    avto.AvtomobiliTest();
    

}

@Test
public void Zivali() throws InterruptedException, AWTException, IOException {
    
    
    MaleZivali mz = new MaleZivali(driver);
    mz.MaleZivaliTest();
    
    
}

@Test
public void Mladi() throws InterruptedException, AWTException, IOException {
    
    
    MladiVoznik mv = new MladiVoznik(driver);
    mv.MladiVoznikTest();
    

}



public WebDriver getDriver() {
    return driver;
}


public void setDriver(WebDriver driver) {
    this.driver = driver;
}


@AfterMethod
public void quit() {
    //driver.close();
}

}

Potovanje.java -> 我运行的测试之一

public class Potovanje {

WebDriver driver;

public Potovanje(WebDriver driver) {
    this.driver = driver;
}

public void PotovanjeTest() throws InterruptedException {
    
    Actions action = new Actions(driver);
    Funkcije funk = new Funkcije(driver);
    Prijave p = new Prijave(driver);
    // This is the login this is what repeats itself 3 times the first time.
    p.ItriglavPrijava();
    
    driver.get("myUrl");
    Thread.sleep(500);
    
    driver.findElement(By.xpath("//*[@id=\"tzModel\"]/div[1]/div[3]/div/div[2]/strong[1]")).click();
    Thread.sleep(500);
    
    driver.findElement(By.xpath("/html/body/form/div[1]/div/div[1]/div[3]/div/div[1]/div/label")).click();
    Thread.sleep(500);
    driver.findElement(By.xpath("/html/body/form/div[1]/div/div[3]/div[3]/input")).click();
    
    // KORAK 1
            
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[8]/div/div[3]/input");
    
    // KORAK 2
    
    Thread.sleep(1000);
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[7]/div[1]/div/div[1]/label[1]/span");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[7]/div[2]/div/div[1]/label[1]/span");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[8]/div/div[3]/input");
    
    Thread.sleep(3000);
    
    WebElement element = driver.findElement(By.xpath("/html/body/form/div[2]/div/div[8]/div/div/div[1]/label[1]/span"));
    
    if(element.isDisplayed()) {
        Thread.sleep(500);
        element.click();
        Thread.sleep(500);
        funk.ScrollInKlik("/html/body/form/div[2]/div/div[9]/div/div[3]/input");
    } 
    
    
    // KORAK 3
    
    Thread.sleep(1000);
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[2]/div[2]/div[1]/div[1]/label[1]/span");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[2]/div[2]/div[2]/div[1]/label[1]/span");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[3]/div/div[3]/input");
    
    // KORAK 4
    
    Thread.sleep(1000);
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[2]/div[4]/div[2]/div/div/p/label");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[2]/div[7]/div[2]/div[1]/label[1]/span");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div/div[3]/div[1]/input");
    
    Thread.sleep(500);
    
    funk.ScrollInKlik("/html/body/form/div[2]/div[1]/div[3]/div[2]/div[1]/div/div/div[1]/label");
    
    funk.ScrollInKlik("/html/body/form/div[2]/div[2]/div/div/a[1]");
    
}

}

test.xml

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

<suite name="Testni suite" parallel="methods" thread-count="2" verbose="1">

    <test name="test">
        <classes>
            <class name="iTriglavTesti.Main"></class>
        </classes>
    </test>

</suite>

我还尝试创建单独的测试用例并将其扩展到 main 而不是仅在方法之前和之后进行测试,我得到了同样的错误。

SklepanjeTestCase.java

public class SklepanjeTestCase extends Main {

@Test
public void DomTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    //login 1
    p.ItriglavPrijava();
    //login 2
    p.SklepanjePrijava();
    
    Dom d = new Dom(getDriver());
    d.DomTest();
}

@Test
public void KarticeTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    BancneKartice kart = new BancneKartice(getDriver());
    kart.BancneKarticeTest();
}

@Test
public void MikroTest() throws InterruptedException, AWTException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    Mikromobilnost m = new Mikromobilnost(getDriver());
    m.MikromobilnostTest();
}

@Test
public void MladiVoznikTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    MladiVoznik m = new MladiVoznik(getDriver());
    m.MladiVoznikTest();
}

@Test
public void NapraveTest() throws InterruptedException, AWTException  {
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    Naprave n = new Naprave(getDriver());
    n.NapraveTest();
    
}

@Test
public void NezTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    Nezgoda n = new Nezgoda(getDriver());
    n.NezgodaTest();
}

@Test
public void OdpTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    OdpovedPotovanj odp = new OdpovedPotovanj(getDriver());
    odp.OdpovedPotovanjTest();
}

@Test
public void OsebTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    OsebnaZascita os = new OsebnaZascita(getDriver());
    os.OsebnaZascitaTest();
}

@Test
public void PotovanjeTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    Potovanje pot = new Potovanje(getDriver());
    pot.PotovanjeTest();
}

@Test
public void ZivaliTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    MaleZivali z = new MaleZivali(getDriver());
    
    z.MaleZivaliTest();
    
}


@Test
public void AvtoTest() throws InterruptedException {
    
    Prijave p = new Prijave(getDriver());
    
    p.ItriglavPrijava();
    p.SklepanjePrijava();
    
    Avtomobili avto = new Avtomobili(getDriver());
    avto.AvtomobiliTest();
}

}

Sklepanje.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<test name="Isklepanje" parallel="methods" thread-count="2">
    <classes>
        <class name="iTriglavTesti.SklepanjeTestCase"></class>
    </classes>
</test>
java selenium-webdriver testng
© www.soinside.com 2019 - 2024. All rights reserved.