运行结果如图7-21所示。
图7-21 执行结果
7.2.3 购买商品功能的测试代码
根据表7-3所示的测试用例,可以使用程序清单7-11至程序清单7-15对购买商品功能进行测试
程序清单7-11 Selenium IDE代码
程序清单7-12 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/product/152026.html); Console.WriteLine("检查是否成功进入商品详细页面:{0}", "http://www.360buy. com/ product/152026.html" == selenium.GetLocation()); //第2步 String ItemName = selenium.GetText("//div[@id='name']"); selenium.Click("link=加入购物车"); selenium.WaitForPageToLoad("30000"); Console.WriteLine("检查是否出现“商品已加入购物车”提示:{0}", selenium. IsTextPresent("商品已成功加入购物车!")); //第3步 selenium.Click("link=继续购物"); Thread.Sleep(5000); Console.WriteLine("返回之前的商品详细页面:{0}", "http://www.360buy. com/ product/152026.html" == selenium.GetLocation()); //第4步 selenium.GoBack(); Thread.Sleep(5000); selenium.Click("link=去结算"); Thread.Sleep(7000); Console.WriteLine("进入购物车结算页面:{0}", "我的购物车 - 京东商城" == selenium. GetTitle()); Console.WriteLine("显示之前添加的商品:{0}", selenium. IsTextPresent (ItemName));
//第5步 selenium.Click("link=去结算"); Thread.Sleep(7000); Console.WriteLine("检查是否进入订单填写页面:{0}", "订单信息确认" == selenium. GetTitle()); selenium.Stop(); Console.ReadKey(); } } } |