编写自动化测试代码—Selenium自动化测试指南(11)

发表于:2013-9-09 11:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:赵卓    来源:51Testing软件测试网

  程序清单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();
}
}
}

52/5<12345>
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • cxwtomcat
    2014-6-08 10:51:34

    这里看看就行了,书嘛凑字数太严重了,鄙视作者出版社和51testing

  • zm51testing
    2013-9-13 15:22:27

    不好意思哈!老师:您的《编写自动化测试代码—Selenium自动化测试指南(11)》第五页的【程序清单7-8  Selenium 1 C#代码】是不是应该改写成【程序清单7-8  Selenium 1 java代码】

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号