博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenuim Webdriver notes
阅读量:4624 次
发布时间:2019-06-09

本文共 9906 字,大约阅读时间需要 33 分钟。

 

Selenium

Selenium resources:     

                Selenium IDE

                Selenium server

                Selenium client drivers and java doc:

Selenium WebDriver: API Commands and Operations 

Fetching a Page:
org.openqa.selenium.WebDriver driver = new FirefoxDriver();
driver.get("");
Locating UI Elements (WebElements):
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
WebElement cheese = driver.findElement(By.name("cheese"));
User Input - Filling In Forms

//input and submit

ebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();

//option

WebElement select = driver.findElement(By.tagName("select"));

List<WebElement> allOptions = select.findElements(By.tagName("option"));

for (WebElement option : allOptions) {

        System.out.println(String.format("Value is: %s", option.getAttribute("value")));

        option.click();

}

Select select = new Select(driver.findElement(By.tagName("select")));

select.deselectAll();

select.selectByVisibleText("Edam")

//button

driver.findElement(By.id("submit")).click();

Moving Between Windows and Frames

driver.switchTo().window("windowName");

for (String handle : driver.getWindowHandles()) {//iterate over every open window

    driver.switchTo().window(handle);// pass a “window handle” to the “switchTo().window()” method

}

driver.switchTo().frame("frameName");

driver.switchTo().frame("frameName.0.child");//access subframes

Popup Dialogs

Alert alert = driver.switchTo().alert();

Navigation: History and Location

driver.navigate().to("");//“navigate().to()” and “get()” do exactly the same thing

driver.navigate().forward();

driver.navigate().back();

Cookies

Drag And Drop

WebElement element = driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

Driver Specifics and Tradeoffs

WebDriver driver = new FirefoxDriver();

WebDriver driver = new InternetExplorerDriver();

WebDriver driver = new ChromeDriver();

WebDriver: Advanced Usage

Explicit Waits

WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){

        @Override

        public WebElement apply(WebDriver d) {

               return d.findElement(By.id("myDynamicElement"));

        }});

Implicit Waits

WebDriver driver = new FirefoxDriver();

//The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("");

WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

Set browser driver

System.setProperty("webdriver.chrome.driver","D:/selenium-server/drivers/chromedriver.exe");

Actions

org.openqa.selenium.interactions.Actions;
Actions: Actions action=new Actions(driver);
 action.click();
click(WebElement)
clickAndHold(WebElement)
doubleClick(WebElement)
dragAndDrop(WebElement source, WebElement target)
contextClick(WebElement)
keyDown(Keys)
keyUp(Keys)
release(WebElement)
sendKeys()
moveByOffset
moveToElement(WebElement)
switchTo()
driver.switchTo().window(nameOrHandle);
driver.switchTo().frame(nameOrId);
driver.switchTo().alert();

quit vs close

webDriver.close() - closes the current window
webDriver.quit() - Quits this driver, closing every associated window.
If there is just one window open the close() and quit() technically do the same.

org.openqa.selenium.By static methods

By.className(java.lang.String className) Finds elements based on the value of the "class" attribute.
By.cssSelector(java.lang.String selector) Finds elements via the driver's underlying W3 Selector engine.
By.id(java.lang.String id)
By.linkText(java.lang.String linkText)
By.name(java.lang.String name)
By.partialLinkText(java.lang.String linkText)
By.tagName(java.lang.String name)
By.xpath(java.lang.String xpathExpression)
abstract java.util.List<WebElement>       findElements(SearchContext context) Find many elements.

Get information of the current page

driver.getCurrentUrl();
driver.getTitle();
driver.getPageSource();

org.openqa.selenium.WebDriver.Navigation

back()
forward()
refresh()
to(String)

Wait

org.openqa.selenium.support.ui.WebDriverWait wait = new WebDriverWait(driver, 5));
org.openqa.selenium.support.ui.ExpectedCondition e = new ExpectedCondition<Boolean>(){
    public Boolean apply(WebDriver d) {
        return d.getTitle().toLowerCase().startsWith("customer");
    }
};
wait.until(e);
//org.openqa.selenium.support.ui.ExpectedConditions
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));
(new WebDriverWait(driver, 5)).until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        return d.getTitle().toLowerCase().startsWith("customer");
    }
});
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id("myDynamicElement"));
}});

Questions

http://blog.csdn.net/shandong_chu/article/category/610439

  • 如何打开一个测试浏览器
  • 如何打开1个具体的url
  • 如何关闭浏览器
  • 如何返回当前页面的url
  • 如何返回当前页面的title
  • 如何执行一段js脚本
  • 定位单个对象
  • 层级定位
    •   WebElement element = driver.findElement(By.name("q")).findElement(By.id("1"));
  • 如何定位frame中的元素
    •   driver.switchTo().frame(nameOrId);
  • 如何捕获弹出窗口
    •   使用windowhandles方法获取所有弹出的浏览器窗口的句柄,然后使用windowhandle方法来获取当前浏览器窗口的句柄,将这两个值的差值就是新弹出窗口的句柄。在获取新弹出窗口的句柄后,使用switch_to.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。
  • 如何处理alert和confirm
    •   driver.switchTo().alert().accept();
  • 使用Page Object设计模式
    •   将一些测试对象以及操作这些测试对象的动作或步骤封装在1个类中,代码的灵活性和适用性将会更强。
  • 如何操作select下拉框
  • 如何智能的等待页面加载完成
1 package selenium.example; 2  3 import org.openqa.selenium.By; 4  5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 import org.openqa.selenium.interactions.Actions;10 import org.openqa.selenium.support.ui.ExpectedCondition;11 import org.openqa.selenium.support.ui.ExpectedConditions;12 import org.openqa.selenium.support.ui.WebDriverWait;13 14 public class Selenium2Example  {15     public static void main(String[] args) {16         System.setProperty("webdriver.chrome.driver","D:/selenium-server/drivers/chromedriver.exe");17         // Create a new instance of the Firefox driver18         // Notice that the remainder of the code relies on the interface, 19         // not the implementation.20         WebDriver driver = new ChromeDriver();21 22         // And now use this to visit Google23         driver.get("http://www.google.com");24         // Alternatively the same thing can be done like this25         // driver.navigate().to("http://www.google.com");26 27         // Find the text input element by its name28         WebElement element = driver.findElement(By.name("q"));29         //WebElement element = driver.findElement(By.name("q")).findElement(By.id("1"));30 31         // Enter something to search for32         element.sendKeys("Cheese!");33 34         // Now submit the form. WebDriver will find the form for us from the element35         element.submit();36 37         // Check the title of the page38         System.out.println("Page title is: " + driver.getTitle());39         40         // Google's search is rendered dynamically with JavaScript.41         // Wait for the page to load, timeout after 10 seconds42         (new WebDriverWait(driver, 10)).until(new ExpectedCondition
() {43 public Boolean apply(WebDriver d) {44 return d.getTitle().toLowerCase().startsWith("cheese!");45 }46 });47 48 // Should see: "cheese! - Google Search"49 System.out.println("Page title is: " + driver.getTitle());50 51 //Close the browser52 driver.quit();53 //driver.close();54 55 //driver.switchTo().window(nameOrHandle);56 //driver.switchTo().frame(nameOrId);57 driver.switchTo().alert().accept();58 driver.navigate();//An abstraction allowing the driver to access the browser's history and to navigate to a given URL.59 60 Actions action=new Actions(driver);61 //action.dragAndDrop(source, target);62 63 driver.getCurrentUrl();64 driver.getTitle();65 driver.getPageSource();66 driver.getWindowHandle();67 68 WebDriverWait wait= new WebDriverWait(driver, 5);69 ExpectedCondition e= new ExpectedCondition
(){70 public Boolean apply(WebDriver d) {71 return d.getTitle().toLowerCase().startsWith("customer");72 }73 };74 wait.until(e);75 76 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));77 }78 }
Selenium Example
import org.openqa.selenium.WebElement;import org.openqa.selenium.ie.InternetExplorerDriver;public class WebTableExample {    public static void main(String[] args)     {        WebDriver driver = new InternetExplorerDriver();        driver.get("http://localhost/test/test.html");        WebElement table_element = driver.findElement(By.id("testTable"));        List
tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr")); System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size()); int row_num,col_num; row_num=1; for(WebElement trElement : tr_collection) { List
td_collection=trElement.findElements(By.xpath("td")); System.out.println("NUMBER OF COLUMNS="+td_collection.size()); col_num=1; for(WebElement tdElement : td_collection) { System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText()); col_num++; } row_num++; } }}
HTML Table get text from tr/td

 

 

 

 

转载于:https://www.cnblogs.com/markjiao/archive/2013/05/27/3101877.html

你可能感兴趣的文章
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>
android编译遇到问题修改
查看>>
解决Ubuntu18.04.2远程桌面Xrdp登录蓝屏问题
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>