web開發ide,Selenium ide及webDriver使用

 2023-12-07 阅读 23 评论 0

摘要:官方網站:http://seleniumhq.org/ Selenium-IDE于firefox上腳本測試。并可以通過該插件生成對應Java webDriver適用的Junit測試代碼。下面是根據Firefox上錄制產生的Junit代碼,部分找HTML的ELEMENT時候代碼需要自己手動更改。 View Code 1 package com.example.tests;
官方網站:http://seleniumhq.org/
Selenium-IDE于firefox上腳本測試。并可以通過該插件生成對應Java webDriver適用的Junit測試代碼。

下面是根據Firefox上錄制產生的Junit代碼,部分找HTML的ELEMENT時候代碼需要自己手動更改。
View Code
 1 package com.example.tests;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 import org.junit.*;
 5 import static org.junit.Assert.*;
 6 import org.openqa.selenium.*;
 7 import org.openqa.selenium.firefox.FirefoxDriver;
 8 import org.openqa.selenium.support.ui.Select;
 9 
10 public class TestDemo {
11     private WebDriver driver;
12     private String baseUrl;
13     private StringBuffer verificationErrors = new StringBuffer();
14     @Before
15     public void setUp() throws Exception {
16 //        driver = new HtmlUnitDriver();
17         driver = new FirefoxDriver();
18         baseUrl = "http://localhost:8080/";
19         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
20     }
21 
22     @Test
23     public void test() throws Exception {
24         driver.get(baseUrl + "/pcloud-web/serve/login.do");
25         driver.findElement(By.id("userId")).clear();
26         driver.findElement(By.id("userId")).sendKeys("hello@163.com");
27         driver.findElement(By.id("fullName")).clear();
28         driver.findElement(By.id("fullName")).sendKeys("hello@163.com");
29         driver.findElement(By.id("mobile")).clear();
30         driver.findElement(By.id("mobile")).sendKeys("13932132121");
31         driver.findElement(By.id("submit")).click();
32         driver.findElement(By.id("phone")).clear();
33         driver.findElement(By.id("phone")).sendKeys("13932132121");
34         driver.findElement(By.id("phone-check")).click();
35         driver.findElement(By.id("phone-check")).clear();
36         driver.findElement(By.id("phone-check")).sendKeys("13932132121");
37         new Select(driver.findElement(By.id("face-select"))).selectByVisibleText("10元");
38         Thread.sleep(1000);
39         //開始直接購買
40         driver.findElement(By.id("direct-charge")).click();
41         Thread.sleep(5000);
42         for (String handle : driver.getWindowHandles()) {
43             driver.switchTo().window(handle);
44         }
45         driver.findElement(By.xpath("(//a[contains(text(),'我的訂單')])[2]")).click();
46         Thread.sleep(1000);
47         try {
48             assertTrue(driver.findElement(By.cssSelector("td[class='status']")).getText().equals("成功"));
49         } catch (Error e) {
50             verificationErrors.append(e.toString());
51         }
52     }
53 
54     @After
55     public void tearDown() throws Exception {
56         driver.quit();
57         String verificationErrorString = verificationErrors.toString();
58         if (!"".equals(verificationErrorString)) {
59             fail(verificationErrorString);
60         }
61     }
62 
63     private boolean isElementPresent(By by) {
64         try {
65             driver.findElement(by);
66             return true;
67         } catch (NoSuchElementException e) {
68             return false;
69         }
70     }
71 }

?

?

轉載于:https://www.cnblogs.com/hzcxy/archive/2012/12/19/2824694.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/193060.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息