乗り越えられない問題があります。Selenium を使い始めたばかりで、簡単な JUnit テストを作成しました。(CMS管理パネルを開き、ログインしてログアウトします。)正しいユーザー名とパスワードを使用すると、魅力的に機能しますが、そうでない場合は、「ログアウト」ボタンを見つけるのを乗り越えることができません。私はそれをtry-catchブロックに入れました。:/ 実際には、例外をスローせずに 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;
}
}
}