Selenium 上机实验说明
1, 安装 SeleniumIDE 插件
下载 Firefox 43.0.1, 添加组件 seleniumIDE, 打开该组件, 右上角红色圆为空心时为录制状态, 实心时为停止录制:
2, 使用 SeleniumIDE 录制脚本和导出脚本
录制完成后, 可以导出 Test Case:
导出的 java 代码为:
- package com.example.tests;
- import java.util.regex.Pattern;
- import java.util.concurrent.TimeUnit;
- import org.junit.*;
- import static org.junit.Assert.*;
- import static org.hamcrest.CoreMatchers.*;
- import org.openqa.selenium.*;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.Select;
- public class TestSelenium {
- private webDriver driver;
- private String baseUrl;
- private boolean acceptNextAlert = true;
- private StringBuffer verificationErrors = new StringBuffer();
- @Before
- public void setUp() throws Exception {
- driver = new FirefoxDriver();
- baseUrl = "https://psych.liebes.top/";
- driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- }
- @Test
- public void testSelenium() throws Exception {
- driver.get(baseUrl + "/st");
- driver.findElement(By.id("username")).clear();
- driver.findElement(By.id("username")).sendKeys("3015218071");
- driver.findElement(By.id("password")).clear();
- driver.findElement(By.id("password")).sendKeys("218071");
- driver.findElement(By.id("submitButton")).click();
- }
- @After
- public void tearDown() throws Exception {
- driver.quit();
- String verificationErrorString = verificationErrors.toString();
- if (!"".equals(verificationErrorString)) {
- fail(verificationErrorString);
- }
- }
- private boolean isElementPresent(By by) {
- try {
- driver.findElement(by);
- return true;
- } catch (NoSuchElementException e) {
- return false;
- }
- }
- private boolean isAlertPresent() {
- try {
- driver.switchTo().alert();
- return true;
- } catch (NoAlertPresentException e) {
- return false;
- }
- }
- private String closeAlertAndGetItsText() {
- try {
- Alert alert = driver.switchTo().alert();
- String alertText = alert.getText();
- if (acceptNextAlert) {
- alert.accept();
- } else {
- alert.dismiss();
- }
- return alertText;
- } finally {
- acceptNextAlert = true;
- }
- }
- }
3, 编写 Selenium Java WebDriver 程序, 测试 input.xlsx 表格中的学号和 git 地址的对应关系是否正确.
来源: http://www.bubuko.com/infodetail-2560377.html