Selenium 学习笔记
WebDriver
What:
- each of browser has its own browser driver
- each has an API maintained by the browser vendor not the selenium
- Drivers inplemented in same language as browser
- Wrapper around browser driver (封装好的,不用懂内部构造)
Goal why
- Quickly and easily write automated test
- Maintain a standardized API that is user friendly
- Emulate actions of users
Who:
- to test across multiple browsers and platforms
- a customizable and powerful framework
How
- using client server communication
- a new session of browser is created, browser window is lanuched
- for each request of Test script, a request is sent to a Web Driver API
- The webDriver API interpreters the request and execute in the browser
- when each step completed, responses are sent to the API and the test script
WorkFlow:
1 | import org.openqa.selenium.chrome.ChromeDriver; |
在代码中 new ChromeDriver() 时,selenium会随机挑选一个端口调用chromedriver程序,调用成功后 chromedriver 会在指定的端口启动一个服务(会有一个进程)
1
2
3 > >tasklist | find "chromedriver"
> chromedriver.exe 7848 Console 1 13,740 K
>
selenium 中使用 apache 的 commons-exec 来运行 chromedriver.exe 启动 ChromeDriver 服务
直接在控制台运行 chromedriver.exe 时,默认端口是9515selenium 通过指定的端口和约定的协议来和 ChromeDriver 进行通信,一个ChromeDriver可用管理多个chrome。
1
2
3
4
5
6
7
8
9 > >tasklist | find "chrome"
> chromedriver.exe 14692 Console 1 14,888 K
> chrome.exe 20952 Console 1 72,204 K
> chrome.exe 7288 Console 1 9,052 K
> chrome.exe 15524 Console 1 10,000 K
> chrome.exe 13036 Console 1 96,028 K
> chrome.exe 11836 Console 1 29,836 K
> chrome.exe 2788 Console 1 59,876 K
>
selenium 中多个 WebDriver 实例对应一个 chromedriver 进程,一个 chromedriver 进程管理多个 chrome 进程。
一个 WebDriver 实例对应一个浏览器窗口。作者:冬瓜baba
链接:https://www.jianshu.com/p/31c8c9de8fcd
来源:简书
接受各种自动化测试的网站
http://formy-project.herokuapp.com/
各种测试网站元素
- Text and button
1 | public class KeyboardAndMouseInput { |
- autocomplete
1 | public class Autocomplete { |
如果报错,就是脚本跑太快了,没有预留足够的时间让网页弹出选项。
Scroll to the element
对于测试来说,如果要测试一个没有显示出来(需要滚动屏幕)的元素,滚动屏幕这个动作并不是必须的。因为对于网页来说,无论在不在屏幕范围内,html里都是有这个元素的。
1 | public class Scroll { |
- Switch to active window
1 | public class SwitchToActiveWindow { |
- switch to alert
1 | public class SwitchToAlert { |
JavaScript Excecuter
When to use:
- When WebDriver fails to click on any element
- To trigger actions on a page
- To perform movement on a page, like scroll
1 | public class ExecuteJavascript { |
Using CSS selector to find elements
1 | // 1. by Id |
Match Text
1 | // 1. by exact Match |
Child Node
1 | <ul id="list"> |