【发布时间】:2015-06-14 20:45:54
【问题描述】:
我想通过单击添加帐户按钮在新标签上打开一个 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(5, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
//try to open account on another tab.
String myWindowHandel= driver.getWindowHandle();
driver.switchTo().window(myWindowHandel);
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
// Take screenshot if any testcase is not working.
System.out.println("Taking Screen Shot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\softs\\eclipse-jee-kepler-SR1-win32\\Workspace\\AutomationFramework\\Screenshot\\screenshot.jpeg"));
}
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));
}
return temp;
}
}
【问题讨论】:
标签: java selenium selenium-webdriver