测试进行了两次,并且没有关闭

时间:2019-02-19 11:41:24

标签: java selenium automation automated-tests

我有一个在Selenium上运行的Java自动化脚本,由于某种原因,该脚本似乎初始化了两次。第一个脚本通过但未关闭,这将导致第二次运行相同的脚本,该脚本也将通过但确实关闭。

这在我运行的所有脚本上都会发生,从而导致窗口的打开和关闭(非常混乱)

我创建了一个拆卸方法,如下所示。这是我的Selenium设置课程的一部分

package support;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class SeleniumSetup {

    protected WebDriver driver;

    public SeleniumSetup(WebDriver driver)
    {

    }

    public SeleniumSetup() {

    }

    public void prepareBrowserForSelenium() {

//      setup();
        if(DriverSingleton.getDriver() == null)
        {
            setup();    
        }
        else
        {
            driver = DriverSingleton.getDriver();
        }

    }

    public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Selenium and drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://the-internet.herokuapp.com/");
        driver.manage().window().maximize();

    }


    public void tearDown() {
        driver.close();
    }

}

这是我的BasePage代码

package pages;

import org.openqa.selenium.WebDriver;

public class BasePage {

    protected WebDriver driver;

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

}

这是我用于运行脚本的类:

package support;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
//      features = "src/test/java/features/ABTestingPage.feature"
//      features = "src/test/java/features/Checkboxes.feature"
//      features = "src/test/java/features/Dropdown.feature"
//      features = "src/test/java/features/AutomationLandingPage.feature"
        features = "src/test/java/features/ChallengingDOM.feature"

//      features = "src/test/java/@ABTestingPage"


        ,glue={"steps"}
        )

public class TestRunner {

}

这是我的步骤,其中包括我的SeleniumSetup类

package steps;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pages.ChallengingDOMPage;
import pages.DropdownPage;
import support.SeleniumSetup;

public class ChallengingDOMSteps extends SeleniumSetup{

    public ChallengingDOMSteps()throws Exception{
        prepareBrowserForSelenium();    
    }

    @Given("^I am on the landing page$")
    public void i_am_on_the_landing_page() throws Throwable {
        new ChallengingDOMPage(driver).landingPage();
    }

    @When("^I select \"(.*?)\"$")
    public void i_select(String arg1) throws Throwable {
        new ChallengingDOMPage(driver).selectChallengingDOM();  
    }

    @Then("^I will be navigated to the \"(.*?)\" page$")
    public void i_will_be_navigated_to_the_page(String arg1) throws Throwable {
        if(arg1.equalsIgnoreCase("Challenging DOM"))
        new  ChallengingDOMPage(driver).verifyChallegingDOMPage();

        else if(arg1.equals("Dropdown"))
         new DropdownPage(driver).verifyDropdownHeading();
     }
}

我认为该步骤的代码还可以。

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import junit.framework.Assert;

public class ChallengingDOMPage extends BasePage {

    public ChallengingDOMPage(WebDriver driver) {
        super(driver);
    }

        By landingPageCopy = By.className("heading");


    public void landingPage() {

        driver.findElement(landingPageCopy).getText();

        System.out.println("verified copy on landing page on DOM test");

    }

    public void selectChallengingDOM() {

        driver.findElement(By.linkText("Challenging DOM")).click();

        System.out.println("DOM URL clicked");

    }

    public void verifyChallegingDOMPage() {

        Assert.assertTrue(driver.findElement(By.xpath("//*[contains(text(), 'The hardest part')]")).isDisplayed());
        System.out.println("Copy Verified on the DOM page");

    }

}

这是控制台输出

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by cucumber.deps.com.thoughtworks.xstream.converters.collections.TreeMapConverter (file:/C:/Users/awalker/.m2/repository/info/cukes/cucumber-jvm-deps/1.0.3/cucumber-jvm-deps-1.0.3.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of cucumber.deps.com.thoughtworks.xstream.converters.collections.TreeMapConverter
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) on port 43775
Only local connections are allowed.
verified copy on landing page on DOM test
DOM URL clicked
Copy Verified on the DOM page
Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) on port 34425
Only local connections are allowed.

1 Scenarios ([32m1 passed[0m)
4 Steps ([32m4 passed[0m)
0m11.433s

我应该进入此页面http://the-internet.herokuapp.com/,导航至http://the-internet.herokuapp.com/challenging_dom并验证副本

2 个答案:

答案 0 :(得分:0)

chromedriver与其他驱动程序不同,需要这样关闭:

driver.close();
driver.quit();

不只是

driver.close();

答案 1 :(得分:0)

您没有显示功能文件的外观。在您的输出中,您好像运行了4个步骤,但是仅向我们显示了3个步骤的定义。我已经按照您的描述尝试了您的设置,并使用了一个简单的功能文件,其中只有一个场景按照定义的相同顺序运行所有三个步骤,并且运行时不会出现输出问题

Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) 
on port 43775
Only local connections are allowed.
verified copy on landing page on DOM test
DOM URL clicked
Copy Verified on the DOM page

这与您的输出相同,但是您在之后显示了另一个ChromeDriver实例化。 查看实际执行的步骤:您必须在其他三个步骤之后执行另一个步骤,并启动另一个webdriver。

还要注意,您的tearDown()方法永远不会运行。您需要告诉脚本在方案完成后运行它。您可以通过添加

@After
public void tearDown() {
    super.tearDown();
}

在您的ChallengingDOMSteps中,请注意它应该是import cucumber.api.java.After;而不是import org.junit.After;,以便Cucumber可以将其捡起。