程序清单7-8 Selenium 1 C#代码
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(); //第1步 selenium.open(http://www.360buy.com/); System.out.println("检查是否成功进入京东首页:" + "http://www. 360buy.com/". equals (selenium.getLocation())); //第2步 selenium.type("id=key", "sdfgasfdgdsfgdsfgdsfg"); selenium.click("//input[@value='搜索']"); selenium.waitForPageToLoad("30000"); System.out.println("页面上出现“抱歉,没有找到与‘sdfgasfdgdsfgdsfgdsfg’相关的商品。”等字样:" + selenium.isTextPresent("抱歉,没有找到与“sdfgasfdgdsfgdsfgdsfg”相关的商品")); //第3步 selenium.type("id=key", "收集"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("页面上出现“您要找的是不是:手机”:" + "您要找的是不是:手机".equals (selenium.getText("id=correctbox"))); //第4步 selenium.type("id=key", "团购"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("进入京东团购页面,URL中包含“http://tuan.360buy.com/”:" + "http: //tuan.360buy.com/chengdu-0-0-0-0-0-0-1-0-0.html".equals(selenium.getLocation())); //第5步 selenium.goBack(); Thread.sleep(2000); selenium.type("id=key", "移动"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("在搜索页面中出现“手机充值”等相关控件:" + selenium. IsElementPresent ("//input[@value='立即充值']")); //第6步 selenium.type("id=key", "图书"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("搜索出来的结果将以列表的形式显示:" + "m psearch plist-book".equals (selenium.getAttribute("//div[@id='plist']/@class"))); //第7步 selenium.type("id=key", "手机"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("搜索出来的结果将以网格的形式显示:" + "m psearch ".equals(selenium. getAttribute("//div[@id='plist']/@class"))); //第8步 selenium.type("id=key", "635085"); selenium.click("//input[@value='搜索']"); Thread.sleep(5000); System.out.println("进入该商品的具体信息页面,URL中包含“http:// www.360buy. com/ product/”:" + "http://www.360buy.com/product/635085.html".equals(selenium.getLocation())); selenium.stop(); } } |
本文选自《Selenium自动化测试指南》第五章节,本站经人民邮电出版社和作者的授权,近期将进行部分章节的连载,敬请期待!
版权声明:51Testing软件测试网获人民邮电出版社和作者授权连载本书部分章节。
任何个人或单位未获得明确的书面许可,不得对本文内容复制、转载或进行镜像,否则将追究法律责任。
相关文章:
自动化测试的实施—Selenium自动化测试指南(10)
购买商品功能的测试代码—Selenium自动化测试指南(12)