Today, we began to introduce automated testing of Web sites based on the open source Selenium tool.
Selenium contains 3 major components, namely:
1. Selenium IDE
The integrated development environment based on Chrome and Firefox extensions can record, replay and export test scripts in different languages.
2. WebDriver
Including a set of class libraries and drivers for different languages and browsers, to identify and operate web pages and their elements in a programmatic way.
3. Selenium Grid
Provides a master-slave architecture, distributed test execution environment, composed of Hub (master) and Node (node).
The old version of Selenium also includes an obsolete RC component. Its principle is to inject JavaScript code into the tested webpage through a proxy to complete the manipulation of the page. Currently, RC has been replaced by the WebDriver component. If you see an article related to this on the Internet, you can just ignore it.
Use Selenium IDE to record automated test scripts
- First install the Chrome browser, and then install the Selenium IDE plug-in
- Click the extension icon (similar to a puzzle symbol) in the upper right corner of the browser, and select to open Selenium IDE;
- In the pop-up window, click the Record a new test in a new project menu item;
- Enter the project name (such as project1) and click OK;
- In the BASE URL text box, enter the Baidu URL
- Click the more icon (3 dots arranged vertically) on the right side of the script name (such as script1), and select the Export menu item;
- Here we give a brief introduction to the script by embedding Java comments. The generated script is as follows, and the waitForWindow method and call used to wait for the window have been ignored.
// 导入Java类库,如selenium、junit等
import org.openqa.selenium.WebDriver;
// 生成的JUnit测试类
public class Test1Test {
// 类级别的私有变量
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
// 使用JUnit的@Before注解,定义在测试执行前,需执行的初始化行为。
@Before
public void setUp() {
// 初始化WebDriver对象
driver = new ChromeDriver();
// 初始化JS执行器对象,用于在浏览器中执行Javascript代码。
js = (JavascriptExecutor) driver;
}
// 使用JUnit的@After注解,定义在测试完成后,需执行的清理动作。
@After
public void tearDown() {
// 退出并清理WebDriver对象
driver.quit();
}
// 使用JUnit的@Test注解,标注此方法为一个测试方法。
@Test
public void test1() {
// 打开百度网站首页
driver.get("https://www.baidu.com/");
// 设置浏览器窗口大小为1440x875
driver.manage().window().setSize(new Dimension(1440, 875));
// 找到id为kw的(文本框)控件,在里面输入“禅道”。
driver.findElement(By.id("kw")).sendKeys("禅道");
// 找到id为su的(按钮)控件,并点击。
driver.findElement(By.id("su")).click();
// 找到指定内容的链接,并点击。
driver.findElement(By.ByPartialLinkText("开源项目管理软件")).click();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。