程序清单7-14 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(http://www.360buy.com/product/152026.html); Console.WriteLine("检查是否成功进入商品详细页面:{0}", http://www.360buy.com/product/152026.html == driver.Url); //第2步 String ItemName = driver.FindElement(By.XPath("//div[@id='name']")).Text; driver.FindElement(By.LinkText("加入购物车")).Click(); Thread.Sleep(5000); Console.WriteLine("检查是否出现“商品已加入购物车”提示:{0}", driver.FindElement (By.CssSelector("BODY")).Text.Contains("商品已成功加入购物车!")); //第3步 driver.FindElement(By.LinkText("继续购物")).Click(); Thread.Sleep(5000); Console.WriteLine("返回之前的商品详细页面:{0}", "http://www.360buy. com/ product/152026.html" == driver.Url); //第4步 driver.Navigate().Back(); Thread.Sleep(5000); driver.FindElement(By.LinkText("去结算")).Click(); Thread.Sleep(7000); Console.WriteLine("进入购物车结算页面:{0}", "我的购物车 - 京东商城" == driver.Title); Console.WriteLine("显示之前添加的商品:{0}", driver.FindElement (By. CssSelector("BODY")).Text.Contains(ItemName)); //第5步 driver.FindElement(By.LinkText("去结算")).Click(); Thread.Sleep(7000); Console.WriteLine("检查是否进入订单填写页面:{0}", "订单信息确认" == driver.Title); driver.Quit(); Console.ReadKey(); } } } |
程序清单7-15 Selenium 2 Java代码
package Project1; import org.openqa.selenium.*; import org.openqa.selenium.WebDriver.*; import org.openqa.selenium.firefox.*; public class Project1Class { public static void main(String[] args) throws InterruptedException { //如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径 System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //第1步 driver.navigate().to(http://www.360buy.com/product/152026.html); System.out.println("检查是否成功进入商品详细页面:" + "http://www.360buy. com/product/ 152026.html".equals(driver.getCurrentUrl())); //第2步 String ItemName = driver.findElement(By.xpath("//div[@id='name']")).getText(); driver.findElement(By.linkText("加入购物车")).click(); Thread.sleep(5000); System.out.println("检查是否出现“商品已加入购物车”提示:" + driver.findElement(By. cssSelector("BODY")).getText().contains("商品已成功加入购物车!")); //第3步 driver.findElement(By.linkText("继续购物")).click(); Thread.sleep(5000); System.out.println("返回之前的商品详细页面:" + "http://www.360buy. com/ product/ 152026.html" .equals( driver.getCurrentUrl())); //第4步 driver.navigate().back(); Thread.sleep(5000); driver.findElement(By.linkText("去结算")).click(); Thread.sleep(7000); System.out.println("进入购物车结算页面:" + "我的购物车 - 京东商城" .equals( driver.getTitle())); System.out.println("显示之前添加的商品:" + driver.findElement (By.cssSelector ("BODY")).getText().contains(ItemName)); //第5步 driver.findElement(By.linkText("去结算")).click(); Thread.sleep(7000); System.out.println("检查是否进入订单填写页面:" + "订单信息确认" .equals( driver. getTitle())); driver.quit(); } } |