[英]Error trying to close a tab and switching to the other tab using selenium / java
[英]Switching to First tab using Selenium Web Driver with Java
將 selenium web 驅動程序與 java 一起使用,我正在嘗試使我單擊鏈接並打開一個新選項卡的功能自動化。 我想為第一個選項卡編寫一個全局方法,在主測試 class 中調用它時會切換到第二個選項卡。 在斷言完成后,我有一個單獨的方法可以在第二個選項卡上執行斷言。 我想編寫另一個全局方法,當調用它時關閉第二個選項卡並切換回第一個選項卡並恢復第一個 window 上的測試。
我編寫了以下方法和測試:
主要測試:
@Test(enabled = true, dataProvider = "TestThis", description = "ClickThere", groups = {"hello" })
public void AttributeValidationOnHelpPage(TestData testData, String Id, String TableName) {
menu.switch(Id);
data = menu.clickData();
data.selectNoneDateRange();
TablePage = data.openTable(TableName);
TablePage.clickAttTab();
menuPage.clickOnHelpPage();
// This method, when called will switch to second tab.
helpPage = menu.switcToHelpPage();
// After swicthing below is the assertions i want to do on the second tab.
Assert.assertTrue(driver.getCurrentUrl().contains("Second page Url."));
String actualValue = helpOnThisPage.getRefTableAttributes();
Assert.assertEquals(actualValue, "Reference Table Attributes", "Reference Table Attributes Missing");
// This method, will close the second tab and switches back to first tab and resumes the test.
helpPage.switchBackTOMainApplication();
}
Global Method for Swicthing to second tab:
public HelpPage switchToHelpPage() {
String originalHandle = driver.getWindowHandle();
try {
TimeUnit.SECONDS.sleep(8);
} catch (InterruptedException ignore) {
}
wait.until(ExpectedConditions.documentReady());
wait.until(ExpectedConditions.isNotLoading());
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) {
driver.switchTo().window(handle);
}
}
return new HelpPage(driver);
}
Global Method for closing the second tab and swicthing back to first tab.
public boolean switchBackTOMainApplication() {
boolean isValid = false;
String originalHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) {
driver.switchTo().window(handle);
}
}
driver.close();
driver.switchTo().window(originalHandle);
return isValid;
}
這是我面臨的問題。 當調用“helpPage.switchBackTOMainApplication()”方法時,它會關閉第一個選項卡而不是切換到它。 我需要做任何修改嗎,這將關閉第二個選項卡並繼續在第一個選項卡上進行測試。
switchBackTOMainApplication() 中的變量“originalHandle”具有第二個選項卡的句柄,因為 go 返回是使用第二個選項卡作為當前 window 執行的。 代碼應更改為:-
public boolean switchBackTOMainApplication() {
boolean isValid = false;
String handleToClose = driver.getWindowHandle();
String mainHandle;
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(handleToClose)) {
mainHandle=handle;
}
}
driver.close();
driver.switchTo().window(mainHandle);
return isValid;
}
當您單擊鏈接時,您應該切換到打開的選項卡並進行驗證
public void switchToNextTab() {
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
之后,您應該返回主選項卡
public void switchTomainTab() {
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(0));
}
如果您需要關閉打開的選項卡然后返回主選項卡,請使用此選項
public void closeTabAndReturn() {
driver.close();
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(0));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.