程序清单7-5 Selenium 2 Java代码
package Project1; import org.openqa.selenium.*; import org.openqa.selenium.WebDriver.*; import org.openqa.selenium.firefox.*; public class Project1Class { public static void main(String[] args) throws InterruptedException { //如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径 System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //第1步 driver.navigate().to(https://passport.360buy.com/new/login.aspx); Thread.sleep(2000); System.out.println("检查是否成功进入用户登录页面:"+ "https://passport. 360buy.com/new/ login.aspx".equals(driver.getCurrentUrl())); //第2步 driver.findElement(By.linkText("找回密码")).click(); Thread.sleep(2000); System.out.println("检查页面是否跳转到http://safe.360buy.com/findPwd/index.action:" + "http://safe.360buy.com/findPwd/index.action".equals(driver.getCurrentUrl())); //第3步 driver.navigate().back(); driver.findElement(By.id("loginname")).clear(); driver.findElement(By.id("loginname")).sendKeys(""); driver.findElement(By.id("loginsubmit")).click(); Thread.sleep(2000); System.out.println("弹出提示“请输入用户名/邮箱/已验证手机”:" + driver.findElement (By.cssSelector("BODY")).getText().contains("请输入用户名/邮箱/已验证手机")); //第4步 driver.findElement(By.id("loginname")).clear(); driver.findElement(By.id("loginname")).sendKeys("erroruser1"); driver.findElement(By.id("loginsubmit")).click(); Thread.sleep(2000); System.out.println("弹出提示“请输入密码”:" + driver.findElement(By.cssSelector("BODY")).getText().contains("请输入密码")); //第5步 driver.findElement(By.id("loginpwd")).clear(); driver.findElement(By.id("loginpwd")).sendKeys("errorpwd1"); driver.findElement(By.id("loginsubmit")).click(); Thread.sleep(2000); System.out.println("弹出提示“您输入的账户名不存在,请核对后重新输入”:" + driver. FindElement (By.cssSelector("BODY")).getText().contains("您输入的账户名不存在,请核对后重新输入")); //第6步 driver.findElement(By.id("loginname")).clear(); driver.findElement(By.id("loginname")).sendKeys("注:这里输入正确的用户名"); driver.findElement(By.id("loginpwd")).clear(); driver.findElement(By.id("loginpwd")).sendKeys("注:这里输入确的用户名密码"); driver.findElement(By.id("loginsubmit")).click(); Thread.sleep(9000); System.out.println("成功登录并跳转到首页,操作栏上出现“您好,xxxx”字样:" + "您好,realzhao![退出]".equals(driver.findElement(By.id("loginbar")).getText())); driver.quit(); } } |
执行结果如图7-20所示。
图7-20 执行结果