在Cucumber Selenium中使用通用步骤时浏览器的多个实例

时间:2019-02-06 09:42:50

标签: selenium selenium-webdriver cucumber cucumber-java

我设置了一个非常简单的测试方案,基本上可以打开浏览器并进行一些导航,并且一切正常。当我将一些步骤分离到common_steps文件中时,浏览器(4x)的多个实例正在打开,由于找不到元素,该测试又使测试失败。

当我将常见步骤复制回原始文件时,测试运行正常。我有点困惑,因为当功能文件中没有多余的步骤或代码行告诉我这样做时,我不明白如何打开多个浏览器。

这是我如何使用它的一个示例。我知道代码有点不整洁,它需要诸如全局等待之类的操作,但是要等到我解决此问题后才能完成,而且这项工作正在进行中。

场景

Scenario Outline: Navigate through Chrome
Given I opened the home page of "<homeUrl>"
Then navigated to my favourite site of "<myFavUrl>"
Then navigated to the Dojo page
When the full catalog was displayed
And the performance option was selected

Examples:
|homeUrl                        |myFavUrl                                   |
|https:\\www.google.co.uk       |https://www.ministryoftesting.com/         |

常量文件

 public class Constant {

    public WebDriver driver;
    public Constant() {
        System.setProperty("webdriver.chrome.driver", "path_to\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    public WebDriver setChromeDriver() {
        if(driver == null) {

            driver = new ChromeDriver();
            driver.manage().window().maximize();
            return driver;
        }else
            return driver;

    }

}

常用步骤文件

public class CommonSteps extends Constant {


    @Given("^I opened the home page of \"([^\"]*)\"$")
    public void navigateToHomePage(String url) throws Throwable{
    driver.get(url);
    driver.manage().window().maximize();
    }

    @Then("^navigated to my favourite site of \"([^\"]*)\"$")
    public void navigateToFavourite(String myFavSite) throws Throwable{
        driver.get(myFavSite);
    }

    //To be used in a separate scenario
    @Given("^I opened the home page of Ministry Of Testing$")
    public void quickLinkToMot() throws Throwable{
        driver.get("https://www.ministryoftesting.com/");
        driver.manage().window().maximize();
    }

}

特定步骤文件

    @Then("^navigated to the Dojo page$")
    public void navigateToDojo() throws Throwable{
        WebDriverWait wait = new WebDriverWait(driver, 5000);
        WebElement djLinkParent = driver.findElement(By.id("navbar-collapse"));
        WebElement djLink = wait.until(ExpectedConditions.elementToBeClickable(djLinkParent.findElement(By.linkText("Dojo"))));
        djLink.click();
    }

    @When("^the full catalog was displayed$")
    public void displayFullCatalog() throws Throwable{
        WebDriverWait wait = new WebDriverWait(driver, 5000);
        WebElement fullCatBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[class='btn btn-xl btn-home']")));
        fullCatBtn.click();
    }

    @And("^the performance option was selected$")
    public void selectPerformance() throws Throwable{
        WebDriverWait wait = new WebDriverWait(driver, 5000);
        WebElement pBtnParent = driver.findElement(By.cssSelector("[class='cat cat2']"));
        WebElement perfLink = wait.until(ExpectedConditions.elementToBeClickable(pBtnParent.findElement(By.xpath("//a[@href='/dojo/lessons?topic=performance']"))));
        perfLink.click();
    }

1 个答案:

答案 0 :(得分:1)

您的项目建设不正确。构造可能会有所不同,但是当您需要使用BDD Cucumber时,这意味着您需要在项目中使用它的概念。然后,您需要遵循最新技术。请在下面看看。

解决方案:

我认为您的问题的解决方案写为here

它告诉您如何在项目中创建架构,因为我认为您的架构是错误的。

我想这就是您要寻找的东西。