import Java.util.concurrent.TimeUnit; importjunit.framework.TestCase; importorg.junit.After; importorg.junit.Before; importorg.junit.Test; importorg.openqa.selenium.Alert; importorg.openqa.selenium.By; importorg.openqa.selenium.NoAlertPresentException; importorg.openqa.selenium.NoSuchElementException; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.firefox.FirefoxDriver; importorg.openqa.selenium.firefox.FirefoxProfile; publicclassSelenium extendsTestCase { privateWebDriver driver; privateString baseUrl; privatebooleanacceptNextAlert = true; privateStringBuffer verificationErrors = newStringBuffer(); @Before publicvoidsetUp() throwsException { FirefoxProfile profile = newFirefoxProfile(); driver = newFirefoxDriver(profile); baseUrl = "http://www.baidu.com"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test publicvoidtestSelenium() throwsException { driver.get(baseUrl + "/"); driver.findElement(By.id("kw")).clear(); driver.findElement(By.id("kw")).sendKeys("JMeter"); driver.findElement(By.id("su")).click(); driver.findElement(By.linkText("jmeter_百度百科")).click(); } @After publicvoidtearDown() throwsException { driver.quit(); String verificationErrorString = verificationErrors.toString(); if(!"".equals(verificationErrorString)) { fail(verificationErrorString); } } privatebooleanisElementPresent(By by) { try{ driver.findElement(by); returntrue; } catch(NoSuchElementException e) { returnfalse; } } privatebooleanisAlertPresent() { try{ driver.switchTo().alert(); returntrue; } catch(NoAlertPresentException e) { returnfalse; } } privateString closeAlertAndGetItsText() { try{ Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if(acceptNextAlert) { alert.accept(); } else{ alert.dismiss(); } returnalertText; } finally{ acceptNextAlert = true; } } } |