gpt4 book ai didi

java - 测试运行了两次并且没有关闭

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:55 25 4
gpt4 key购买 nike

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

这种情况发生在我运行的所有脚本上,从而导致大量窗口打开而不关闭(非常困惑)

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

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();
}

}

这是我的基本页面代码

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并验证副本

最佳答案

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

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

而不仅仅是

driver.close();

关于java - 测试运行了两次并且没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54765512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com