程序清单7-3 Selenium 1 Java代码
package Project1; import com.thoughtworks.selenium.*; //注意这里导入了selenium包中的内容 public class Project1Class { public static void main(String[] args) throws InterruptedException { DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", http://www.google.com); selenium.start(); selenium.open("https://passport.360buy.com/new/login.aspx"); System.out.println("检查是否成功进入用户登录页面:" + "https://passport.360buy.com/new/login.aspx".equals(selenium.getLocation())); //第2步 selenium.click("link=找回密码"); selenium.waitForPageToLoad("30000"); System.out.println("检查页面是否跳转到http://safe.360buy.com/findPwd/index.action:" + "http://safe.360buy.com/findPwd/index.action".equals(selenium.getLocation())); //第3步 selenium.goBack(); Thread.sleep(2000); selenium.type("id=loginname", ""); selenium.click("id=loginsubmit"); Thread.sleep(2000); System.out.println("弹出提示“请输入用户名/邮箱/已验证手机”:" + selenium.isTextPresent("请输入用户名/邮箱/已验证手机")); //第4步 selenium.type("id=loginname", "erroruser1"); selenium.click("id=loginsubmit"); Thread.sleep(2000); System.out.println("弹出提示“请输入密码”:" + selenium.isTextPresent("请输入密码")); //第5步 selenium.type("id=loginpwd", "errorpwd1"); selenium.click("id=loginsubmit"); Thread.sleep(2000); System.out.println("弹出提示“您输入的账户名不存在,请核对后重新输入”:" + selenium. isTextPresent("您输入的账户名不存在,请核对后重新输入")); //第6步 selenium.type("id=loginname", "注:这里输入正确的用户名"); selenium.type("id=loginpwd", "注:这里输入正确的密码"); selenium.click("id=loginsubmit"); selenium.waitForPageToLoad("30000"); Thread.sleep(2000); System.out.println("成功登录并跳转到首页,操作栏上出现“您好,xxxx”字样:" + "您好,realzhao![退出]".equals(selenium.getText("id=loginbar"))); selenium.stop(); } } |
程序清单7-4 Selenium 2 C#代码
using System; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using System.Threading; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); //第1步 driver.Navigate().GoToUrl(https://passport.360buy.com/new/login.aspx); Thread.Sleep(2000); Console.WriteLine("检查是否成功进入用户登录页面:{0}", "https://passport. 360buy. com/new/login.aspx" == driver.Url); //第2步 driver.FindElement(By.LinkText("找回密码")).Click(); Thread.Sleep(2000); Console.WriteLine("检查页面是否跳转到http://safe.360buy.com/findPwd/index.action:{0}", "http://safe.360buy.com/ findPwd/index. action" == driver.Url); //第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); Console.WriteLine("弹出提示“请输入用户名/邮箱/已验证手机”:{0}", driver.FindElement (By.CssSelector("BODY")).Text.Contains("请输入用户名/邮箱/已验证手机")); //第4步 driver.FindElement(By.Id("loginname")).Clear(); driver.FindElement(By.Id("loginname")).SendKeys("erroruser1"); driver.FindElement(By.Id("loginsubmit")).Click(); Thread.Sleep(2000); Console.WriteLine("弹出提示“请输入密码”:{0}", driver.FindElement(By.CssSelector("BODY")).Text.Contains("请输入密码")); //第5步 driver.FindElement(By.Id("loginpwd")).Clear(); driver.FindElement(By.Id("loginpwd")).SendKeys("errorpwd1"); driver.FindElement(By.Id("loginsubmit")).Click(); Thread.Sleep(2000); Console.WriteLine("弹出提示“您输入的账户名不存在,请核对后重新输入”:{0}", driver. FindElement(By.CssSelector("BODY")).Text.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); Console.WriteLine("成功登录并跳转到首页,操作栏上出现“您好,xxxx”字样:{0}", "您好,realzhao![退出]" == driver.FindElement(By.Id("loginbar")).Text); driver.Quit(); Console.ReadKey(); } } } |