【问题标题】:Four identical Selenium test gives different result四个相同的硒测试给出不同的结果
【发布时间】:2020-02-11 20:41:21
【问题描述】:

我有一个包含四个相同测试的测试类(因​​为这个错误)。我不明白为什么其中三个通过得很好,一个给我一个错误。有人可以告诉我问题出在哪里吗?下面是类的代码:

package tests;

import Pages.SinCityPage;
import Pages.SpartaPage;
import model.Sin;
import model.SinTagsEnum;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PageFactoryExampleTest extends Base
{
    protected String siteUrl = "/sincity.php";
    protected String spartaSiteUrl = "/sparta.php";

    protected SinCityPage sinCityPage;

    protected SpartaPage spartaPage;


    @Before
    public void setUp() throws IOException {
        driver.get(baseUrl + siteUrl);
        this.sinCityPage = new SinCityPage(driver);
        this.spartaPage = new SpartaPage(driver);
    }


    @Test
    public void test1() throws InterruptedException {
        Sin sin = this.createSin();
        sinCityPage.openSinDetail(sin);
        sinCityPage.fillForm(sin);
        sinCityPage.openSinDetail(sin);
        sinCityPage.checkSinDetail(sin);

        driver.get(baseUrl + spartaSiteUrl);
        spartaPage.checkSins();
        spartaPage.checkForgive();
    }


    @Test
    public void test2() throws InterruptedException {
        Sin sin = this.createSin();
        sinCityPage.openSinDetail(sin);
        sinCityPage.fillForm(sin);
        sinCityPage.openSinDetail(sin);
        sinCityPage.checkSinDetail(sin);

        driver.get(baseUrl + spartaSiteUrl);
        spartaPage.checkSins();
        spartaPage.checkForgive();
    }


    @Test
    public void test3() throws InterruptedException {
        Sin sin = this.createSin();
        sinCityPage.openSinDetail(sin);
        sinCityPage.fillForm(sin);
        sinCityPage.openSinDetail(sin);
        sinCityPage.checkSinDetail(sin);

        driver.get(baseUrl + spartaSiteUrl);
        spartaPage.checkSins();
        spartaPage.checkForgive();
    }


    @Test
    public void test4() throws InterruptedException {
        Sin sin = this.createSin();
        sinCityPage.openSinDetail(sin);
        sinCityPage.fillForm(sin);
        sinCityPage.openSinDetail(sin);
        sinCityPage.checkSinDetail(sin);

        driver.get(baseUrl + spartaSiteUrl);
        spartaPage.checkSins();
        spartaPage.checkForgive();
    }


    private Sin createSin() {
        List<SinTagsEnum> tags = new ArrayList<SinTagsEnum>();
        tags.add(SinTagsEnum.BLACK_MAIL);
        tags.add(SinTagsEnum.HIJACK);

        return new Sin("Vrazda", "Ja", "Vrazda spojena s lupeznzm prepadnutim muchy v zapadnutej ulicke", tags);
    }
}

这是基类

package tests;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import services.WebDriverService;

import java.io.IOException;

public class Base
{
    protected WebDriver driver;

    protected String baseUrl = "http://seleniumweb2";

    @Before
    public void baseBefore() throws IOException {
        this.driver = (new WebDriverService()).getDriver();
        //this.driver = (new WebDriverService()).getDriver(WebDriverService.GECKO_EXE);
    }


    @After  // It can not be AfterClass because of closing browsers on remote server
    public void baseAfter()
    {
        try {
            //driver.close(); // Closes current active window with focus on. Other windows opened bz driver remain open.
            driver.quit();  // Closes all opened windows by this driver.
        } catch ( Exception e ) {
            System.out.println("Base::baseAfter() throws exception: " + e.getMessage());
        }
    }

}

这里是驱动类

package services;

import model.BrowsersEnum;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;


public class WebDriverService
{

    public static String CHROME_EXE = "chromedriver.exe";
    public static String PHANTOM_EXE = "phantomjs.exe";
    public static String GECKO_EXE = "geckodriver.exe";
    public static String HUB_URL = "http://127.0.0.1:4444/wd/hub";

    private WebDriver driver = null;


    /**
     * This returns singleton driver
     * @return
     * @throws MalformedURLException
     */
    public WebDriver getDriver() throws MalformedURLException {
        String browser = System.getProperty("browser").toLowerCase();

        if (browser.equals(BrowsersEnum.CHROME.getValue())) {
            this.driver = new RemoteWebDriver(this.getRemoteUrl(), new ChromeOptions());
        } else if (browser.equals(BrowsersEnum.FIREFOX.getValue())) {
            this.driver = new RemoteWebDriver(this.getRemoteUrl(), new FirefoxOptions());
        } else {
            throw new IllegalArgumentException("Base::getDriver() invalid argument: " + browser);
        }

        return this.driver;
    }

    /**
     * Avoid using Selenium server GRID
     * @param which
     * @return
     */
    public WebDriver getDriver(String which) {
        if (which == PHANTOM_EXE) {
            System.setProperty("phantomjs.binary.path", PHANTOM_EXE);
            this.driver = new PhantomJSDriver();
        } else if (which == CHROME_EXE) {
            System.setProperty("webdriver.chrome.driver", CHROME_EXE);
            this.driver = new ChromeDriver();
        } else if (which == GECKO_EXE) {
            System.setProperty("webdriver.chrome.driver", GECKO_EXE);
            this.driver = new FirefoxDriver();
        }

        return this.driver;
    }

    /**
     * Remote selenium server (nodes) needs to have new instance for every test (not singleton).
     * Otherwise on the driver.quit() lose the session.
     * @return
     * @throws MalformedURLException
     */
    public URL getRemoteUrl() throws MalformedURLException {
        URL url = new URL( HUB_URL );
        return url;
    }
}

这是 Jenkins 错误日志的截图

这让我发疯。我真的不知道发生了什么。

【问题讨论】:

  • 失败的总是同一个(例如第三个)?
  • 请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
  • 不,失败的不是同一个测试。
  • 必须缺少某些代码。比如driver是什么?
  • @Čamo tearDown() 方法在哪里?您是否错误地尝试了 2 次 quit() 驱动程序?

标签: java selenium testing


【解决方案1】:

我解决了并行测试的 maven surefire 插件设置问题。这是解决方案:

<configuration>
    <forkCount>8</forkCount>
    <reuseForks>false</reuseForks>
</configuration>

之前有

<configuration>
     <parallel>methods</parallel>
     <threadCount>10</threadCount>
</configuration>

【讨论】:

    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2013-10-11
    • 2021-12-26
    相关资源
    最近更新 更多