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

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

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

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

分享:

  7.2.2  搜索商品功能的测试代码

  根据表7-5所示的测试用例,可以用程序清单7-6至程序清单7-10所示的代码对商品搜索功能进行测试。

  程序清单7-6  Selenium IDE代码

  程序清单7-7  Selenium 1 C#代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Selenium;
using System.Threading;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", http://www.google.com);
selenium.Start();
//第1步
selenium.Open("http://www.360buy.com/");
Console.WriteLine("检查是否成功进入京东首页:{0}", http://www.360buy.com/ == selenium.GetLocation());
//第2步
selenium.Type("id=key", "sdfgasfdgdsfgdsfgdsfg");
selenium.Click("//input[@value='搜索']");
selenium.WaitForPageToLoad("30000");
Console.WriteLine("页面上出现“抱歉,没有找到与‘sdfgasfdgdsfgdsfgdsfg’相关的商品。”等字样:{0}", selenium.IsTextPresent("抱歉,没有找到与“sdfgasfdgdsfgdsfgdsfg”相关的商品"));
//第3步
selenium.Type("id=key", "收集");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("页面上出现“您要找的是不是:手机”:{0}", "您要找的是不是:手机" == selenium.GetText("id=correctbox"));
//第4步
selenium.Type("id=key", "团购");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("进入京东团购页面,URL中包含“http://tuan.360buy.com/”:{0}", http://tuan.360buy.com/chengdu-0-0-0-0-0-0-1-0-0.html == selenium.GetLocation());
//第5步
selenium.GoBack();
Thread.Sleep(2000);
selenium.Type("id=key", "移动");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("在搜索页面中出现“手机充值”等相关控件:{0}", selenium. IsElementPresent("//input[@value='立即充值']"));
//第6步
selenium.Type("id=key", "图书");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("搜索出来的结果将以列表的形式显示:{0}", "m psearch plist-book" == selenium.GetAttribute("//div[@id='plist']/@class"));
//第7步
selenium.Type("id=key", "手机");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("搜索出来的结果将以网格的形式显示:{0}", "m psearch " == selenium. GetAttribute("//div[@id='plist']/@class"));
//第8步
selenium.Type("id=key", "635085");
selenium.Click("//input[@value='搜索']");
Thread.Sleep(5000);
Console.WriteLine("进入该商品的具体信息页面,URL中包含“http://www.360buy. com/product/”:{0}", http://www.360buy.com/product/635085.html == selenium.GetLocation());
selenium.Stop();
Console.ReadKey();
}
}
}

54/5<12345>
精选软件测试好文,快来阅读吧~

精彩评论

  • 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号