我有一个问题,我无法通过。我刚刚开始使用Selenium,我用它做了一个简单的JUnit测试。 (打开CMS管理面板,登录并注销。)如果我使用正确的用户名和密码,它就像一个魅力,但如果我不这样做,它就不能过度找到&#34 ;退出"按钮,但我把它放入try-catch块。 :/实际上它只是停止if语句(参见下面的代码)而不抛出异常,它不会关闭浏览器并完成测试,因为它永远不会达到" driver.quit()&# 34。
如果您对我的问题有任何疑问,请帮助我!
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;
}
}
}
答案 0 :(得分:0)
嗯..你正在使用driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
,这意味着隐含等待所有元素。因此,您的程序会等待 30 秒显示该按钮,然后再继续。尝试等待31秒,然后你会看到结果。
答案 1 :(得分:0)
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.");
}