gpt4 book ai didi

java - 运行多个测试时 session ID 无效

转载 作者:行者123 更新时间:2023-12-04 08:37:51 25 4
gpt4 key购买 nike

我之前也提过这个问题,但似乎没有人能够回答。因此,我将继续使用当前的代码结构再次发布一个问题:
我的 parent 有一个 @BeforeEach设置一切的方法:

public class WebDriverSettings {

public static WebDriver driver;

@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
loginToEnvironment();
}


@AfterEach
public void tearDown(){
driver.manage().deleteAllCookies();
driver.close();
}



private void loginToEnvironment() {
String TARGET_URL = System.getProperty("TARGET_URL", "");
String URL = " + "@" + TARGET_URL;
driver.get(URL);
}
比它是 parent 的 child ,测试类之一:
public class LoginServiceTest extends WebDriverSettings {

private LoginModal loginModal;

@BeforeEach
public void setUp() {
super.setUp();
loginModal = HomePage.homepageInstance(driver)
.acceptCookies()
.closeCovidMessage()
.clickOnMyAccountTab()
.switchToLoginTab();
}

@Test
public void shouldSuccessfullyLogin() {
assertEquals(userName, loginModal.login(loginCorrectData()).myAccountName.getText());
}

@Test
public void shouldDisplayIncorrectCredentialsMessage() {
loginModal.login(loginIncorrectData());
assertEquals(incorrectCredentialsMessage,loginModal.wrongCredentials.getText());
}
pom 对象的示例,我所有的 pom 对象都是以这种方式构建的:
ublic class LoginModal extends BasePage {

private static LoginModal loginModal;

@FindBy(xpath = "//input[@name='email']")
public WebElement emailInputField;

@FindBy(xpath = "//input[@name='password']")
public WebElement passwordInputField;

@FindBy(css = ".button-hover-wrapper > div.button-inner")
public WebElement loginButton;

@FindBy(xpath = "//a[text()='Log In']")
public WebElement loginTab;

@FindBy(css = ".error-info")
public WebElement wrongCredentials;

private LoginModal(WebDriver driver) {
super(driver);
}

public static LoginModal loginModalInstance(WebDriver driver) {
return loginModal == null ? loginModal = new LoginModal(driver) : loginModal;
}

public HomePage login(Login login) {
loginModal.clickOnLoginTab()
.fillInEmail(login.getEmail())
.fillInPassword(login.getPassword())
.clickOnLoginButton();
return HomePage.homepageInstance(driver);
}

public LoginModal clickOnLoginTab() {
loginModal.loginTab.click();
return loginModal;
}

public LoginModal fillInEmail(String email) {
loginModal.emailInputField.clear();
loginModal.emailInputField.sendKeys(email);
return loginModal;
}

public LoginModal fillInPassword(String password) {
loginModal.passwordInputField.clear();
loginModal.passwordInputField.sendKeys(password);
return loginModal;
}

public void clickOnLoginButton() {
loginModal.loginButton.click();
}
}
根据我的理解,我的代码执行以下操作: setUp每次测试前都会运行 parent 中的方法,而不是 setUp child中的方法开始运行,成功打开浏览器,完成测试。但是,当它进行另一个测试时,我得到以下信息:
    invalid session id
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'Artjoms-MacBook-Air.local', ip: 'fe80:0:0:0:10eb:9260:8538:39df%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.6', java.version: '15'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 86.0.4240.183, chrome: {chromedriverVersion: 86.0.4240.22 (398b0743353ff..., userDataDir: /var/folders/z6/46cwjtvx2fj...}, goog:chromeOptions: {debuggerAddress: localhost:56802}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 9837fd96a4efca85f4bcdbf90fea8a77
*** Element info: {Using=css selector, value=.cookie-accept-all}
org.openqa.selenium.NoSuchSessionException: invalid session id
当前依赖堆栈:
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.141.5'
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2'
compileOnly 'org.projectlombok:lombok:1.18.16'
annotationProcessor 'org.projectlombok:lombok:1.18.16'
testCompileOnly 'org.projectlombok:lombok:1.18.16'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
}

最佳答案

所以问题是页面对象是以单例方式设计的:

public static LoginModal loginModalInstance(WebDriver driver) {
return loginModal == null ? loginModal = new LoginModal(driver) : loginModal;
}
因此,一旦在第一次测试中使用驱动程序初始化,它们在新测试中仍然引用旧驱动程序实例(已关闭)。
解决方案是返工 loginModalInstance和其他类似的方法,以便它们使用新的驱动程序实例重新初始化现有对象的字段。

关于java - 运行多个测试时 session ID 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64711031/

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