自动登录博客园并且退出登录
package TestNG;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
public class SecondTest {
@Test
public void f() throws InterruptedException {
// System.out.println("hello World!!!");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
driver.findElement(By.id("kw")).sendKeys("博客园");
driver.findElement(By.id("su")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//div[@id='1']/h3/a")).click();
String thisHandleId = driver.getWindowHandle();
for(String TempHandleId : driver.getWindowHandles())
{
if(!TempHandleId.equals(thisHandleId))
{
driver.switchTo().window(TempHandleId);
}
}
driver.findElement(By.linkText("登录")).click();
Thread.sleep(1000);
driver.findElement(By.id("input1")).sendKeys("nieliangcai");
driver.findElement(By.id("input2")).sendKeys("nlc@13661501664");
Thread.sleep(1000);
driver.findElement(By.id("signin")).click();
Thread.sleep(1000);
driver.findElement(By.linkText("退出")).click();
Thread.sleep(1000);
Alert Dialog = driver.switchTo().alert();
Dialog.accept();
System.out.println(driver.getTitle());
}
@BeforeTest
public void beforeTest() {
System.out.println("@BeforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("@AfterTest");
}
}