我遇到了一个无法解决的问题。我刚开始使用 Selenium,并用它做了一个简单的 JUnit 测试。 (打开 CMS 管理面板,登录并注销。)如果我使用正确的用户名和密码,它就像一个魅力,但如果我不这样做,它就无法找到“注销”按钮,但是我已经把它放到 try-catch block 中了。 :/实际上它只是在 if 语句处停止(参见下面的代码)而没有抛出异常,并且它不会关闭浏览器并完成测试,因为它永远不会到达“driver.quit()”。
如果你对我的问题有任何想法,请帮助我!
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 JavaExp {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://urlhere.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testJValami() throws Exception {
driver.get(baseUrl + "/administrator/");
driver.findElement(By.id("mod-login-username")).clear();
driver.findElement(By.id("mod-login-username")).sendKeys("admin");
driver.findElement(By.id("mod-login-password")).clear();
driver.findElement(By.id("mod-login-password")).sendKeys("almakfa");
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button
if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link
System.out.println("Got it.");
} else {
System.out.println("Not found.");
}
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
最佳答案
好吧,问题是在您按下“登录”按钮后页面应该呈现。 所以在你的情况下我会尝试不同的等待机制:
public boolean isElementPresent(By selector)
{
return driver.findElements(selector).size()>0;
}
-
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }
driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button Thread.sleep(1000); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }
public WebElement fluentWait(final By locator){ Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(org.openqa.selenium.NoSuchElementException.class); WebElement foo = wait.until( new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } } ); return foo; } ; driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button fluentWait(By.linkText("Kilépés")); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }
关于java - Selenium JUnit 4 测试 - findElement() 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636909/