我可以在同一浏览器上打开另一个标签,但在打开该页面后,在另一个标签页上我无法执行操作。
当前情况保持在:
但在切换到该窗口后,我想在登录页面上再次提供凭证,但不想关闭登录帐户的第一个打开的选项卡。
我在框架中这样做,所以请检查以下代码,并根据此集成您的代码并帮助我。
private boolean operateWebDriver(String operation, String Locator,
String value, String objectName) throws Exception {
boolean testCaseStep = false;
try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
if (operation.equalsIgnoreCase("newTab")) {
System.out.println("newTab" + temp);
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
//Taking snapshot when test case fail.
System.out.println("Taking snapshot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));
}
return testCaseStep;
}
public WebElement getElement(String locator, String objectName)
throws Exception {
WebElement temp = null;
System.out.println("Locator-->" + locator);
if (locator.equalsIgnoreCase("id")) {
temp = driver.findElement(By.id(objectName));
} else if (locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
System.out.println("xpath temp ----->" + temp);
} else if (locator.equalsIgnoreCase("name")) {
temp = driver.findElement(By.name(objectName));
System.out.println("name temp ----->" + temp);
}
return temp;
}
}
答案 0 :(得分:0)
现在,我可以在另一个标签上打开后处理打开的URL。这是我最终的代码。
private boolean operateWebDriver(String operation, String Locator,
String value, String objectName) throws Exception {
boolean testCaseStep = false;
try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
if (operation.equalsIgnoreCase("newTab")) {
System.out.println("newTab" + temp);
// Open link in new tab.
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(temp).keyUp(Keys.CONTROL).build().perform();
//After Opening link in new tab display that window.
newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();
// Get the number of tab opened in browser.
ArrayList<String> newTab1 = new ArrayList<String>(driver.getWindowHandles());
// change focus to new tab
driver.switchTo().window(newTab1.get(0));
// Now perform the operation here.
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
//Taking snapshot when test case fail.
System.out.println("Taking snapshot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\AutomationFramework\\AutomationFramework\\Screenshots\\screenshot.jpg"));
}
return testCaseStep;
}
答案 1 :(得分:0)
//current window - before opening link on new tab
String winHandleBefore = driver.getWindowHandle();
//write code to open new link
tabs2 = new ArrayList<String> (driver.getWindowHandles());
for( int k= 0 ; k< tabs2.size(); k++ ){
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(tabs2.get(k));
}