文章目录
Selenium IDE 的安装和使用
一、下载与安装
- 安装 Chrome 浏览器、FireFox 浏览器
- 应用商店下载 Selenium IDE(chrome 下载需要科学上网)
二、Selenium IDE 功能界面介绍
三、脚本录制
1、基本步骤
录制脚本,步骤如下(以录制百度为例):
- 输入百度连接:https://www.baidu.com
- 点击录制按钮,开始录制
- 点击百度首页“新闻”连接,后续点击国内,再点击国际
- 点击录制按钮,结束录制
- 点击执行用例按钮,进行脚本回放
- 脚本执行成功后,操作步骤处,会变成绿色。同时会出现Runs:成功总数;Failures:失败总数
2、使用增强脚本
以点击“即时新闻”超链接为例:
添加assertText要素到之前到录制脚本中,如图所示,添加的Command是“assertText”;target是“linkText=即时新闻”;value是“即时新闻”。说明此时的检查点设置是检查页面“即时新闻”字符串。如果有则检查通过,脚本继续执行,如果没有,则检查未通过,脚本停止执行。
如图所示执行日志显示,脚本执行和检查点检查都失败了。
因为“即时新闻”是国际Tab页才会有的,在“新闻”首页找不到这个元素,所以执行失败了
3、断言模式介绍
常见的 3 种模式:assert、verify、waitfor
- assert:(断言)失败时测试终止
- verify:(验证)失败时,测试继续执行,将错误记入日志显示屏
- waitfor:用于等待某些条件变为真,可用于AJAX应用程序测试
assert
- assertLocation:判断当前是在正确的页面
- assertTitle:检查当前页面的 title 是否正确
- assertValue:检查 input 的值, checkbox 或 radio,有值为”on”无为”off”
- assertSelecteted:检查 select 的下拉菜单中选中是否正确
- assertSelectedOptions:检查下拉菜单中的选项的是否正确
- assertText:检查指定元素的文本
- assertTextPresent:检查在当前给用户显示的页面上是否有出现指定的文本
- assertTextNotPresent:检查在当前给用户显示的页面上是否没有出现指定的文本
- assertAttribute:检查当前指定元素的属性的值
- assertTable:检查 table 里的某个 cell 中的值
- assertEditable:检查指定的 input 是否可以编辑
- assertNotEditable:检查指定的 input 是否不可以编辑
- assertAlert:检查是否有产生带指定 message 的 alert 对话框
verify
- verifyTitle:检查预期的页面标题
- verifyTextPresent:验证预期的文本是否在页面上的某个位置
- verifyElementPresent:验证预期的UI元素,它的HTML标签的定义,是否在当前网页上
- verifyText:核实预期的文本和相应的HTML标签是否都存在于页面上
- verifyTable:验证表的预期内容
waitfor
- waitForPageToLoad:暂停执行,直到预期的新的页面加载
- waitForElementPresent:等待检验某元素的存在。为真时,则执行
4、脚本导出
点击用例旁边的三个小点,点击 export ,即可倒出。
目前支持的语言,以及使用的测试框架:
- C# NUnit
- C# xUnit
- Java JUnit
- JavaScript Mocha
- Python pytest
- Ruby RSpec
导出示例,导出文件为 Java
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class SearchTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
public String waitForWindow(int timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
Set<String> whNow = driver.getWindowHandles();
Set<String> whThen = (Set<String>) vars.get("window_handles");
if (whNow.size() > whThen.size()) {
whNow.removeAll(whThen);
}
return whNow.iterator().next();
}
@Test
public void search() {
driver.get("https://www.baidu.com/");
driver.manage().window().setSize(new Dimension(1328, 889));
driver.findElement(By.id("kw")).sendKeys("baidu");
vars.put("window_handles", driver.getWindowHandles());
driver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
vars.put("win3334", waitForWindow(2000));
driver.switchTo().window(vars.get("win3334").toString());
driver.findElement(By.linkText("百度一下,你就知道")).click();
}
}
导出文件为 python
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestSearch():
def setup_method(self, method):
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def wait_for_window(self, timeout = 2):
time.sleep(round(timeout / 1000))
wh_now = self.driver.window_handles
wh_then = self.vars["window_handles"]
if len(wh_now) > len(wh_then):
return set(wh_now).difference(set(wh_then)).pop()
def test_search(self):
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(1328, 889)
self.driver.find_element(By.ID, "kw").send_keys("baidu")
self.vars["window_handles"] = self.driver.window_handles
self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
self.vars["win3334"] = self.wait_for_window(2000)
self.driver.switch_to.window(self.vars["win3334"])
self.driver.find_element(By.LINK_TEXT, "百度一下,你就知道").click()