我们录制了一个使用 Selenium IDE 登录 jira 的测试用例。它运行正确。但是当它导出到 java web 驱动程序(jUnit4)时,它不起作用并给出元素未找到错误。
代码是:
package newjiralogin;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewJiraLogin {
private WebDriver driver;
private String baseUrl;
private final StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://jiratest/";
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
public void testUntitled2() throws Exception {
driver.get("http://jiratest");
driver.findElement(By.id("login-form-username")).clear();
driver.findElement(By.id("login-form-username")).sendKeys("saumlk");
driver.findElement(By.id("login-form-password")).clear();
driver.findElement(By.id("login-form-password")).sendKeys("saumlk");
driver.findElement(By.id("login")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
主要方法类:
package newjiralogin;
import org.junit.runner.JUnitCore;
public class NewJiralogintest {
public static void main(String[] args) throws Exception {
JUnitCore.main("newjiralogin.NewJiraLogin");
}
}
最佳答案
登录表单驻留在 iframe 内,即在访问元素之前,您必须切换驱动程序的上下文,例如
driver.switchTo().frame("gadget-0");
(假设“gadget-0”是包含登录框的框架的 ID)。
我不是 IDE->WebDriver 转换方面的专家,但上下文切换似乎丢失了。如果您想为 JIRA 实现 UI 自动化,请查看 JIRA PageObjects,它确实可以简化许多常见任务。 Atlassian 文档页面或此处提供了如何使用它们的示例:https://blog.codecentric.de/en/2014/07/part-3-agile-testing-jira-plugins-system-tests/ 。这篇博客文章是我们编写的一个简短系列的一部分,因为我们遇到了类似的问题,并希望分享我们学到的东西。
关于java - 使用 Selenium WebAPI 进行 Jira 测试自动化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965683/