本文为霍格沃兹测试开发学社学员学习笔记分享
原文链接:https://ceshiren.com/t/topic/24832
安装完成后,通过在浏览器的菜单栏中点击它的图标来启动它:
如果没看到图标,首先确保是否安装了Selenium IDE扩展插件
通过以下链接访问所有插件
用例标题 | 类型 | 前提条件 | 用例步骤 | 预期结果 | 实际结果 |
---|---|---|---|---|---|
搜狗搜索功能 | 正例 | 进入搜狗首页 | 1. 输入搜索关键词 2.按下回车键 | 1. 搜索成功 2. 搜索结果列表包含关键字 |
自动化测试用例 | 作用 | |
---|---|---|
用例标题 | 测试包、文件、类、方法名称 | 用例的唯一标识 |
前提条件 | setup、setup_class(Pytest); | |
BeforeEach、BeforeAll(JUnit) | 测试用例前的准备动作,比如读取数据或者driver的初始化 | |
用例步骤 | 测试方法内的代码逻辑 | 测试用例具体的步骤行为 |
预期结果 | assert 实际结果 = 预期结果 | 断言,印证用例是否执行成功 |
实际结果 | assert 实际结果 = 预期结果 | 断言,印证用例是否执行成功 |
后置动作 | teardown、teardown_class(Pytest); | |
@AfterEach、@AfterAll(JUnit) | 脏数据清理、关闭driver进程 |
脚本步骤:
# 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 Test():
def setup_method(self, method):
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_sougou(self):
# 打开网页,设置窗口
self.driver.get("https://www.sogou.com/")
self.driver.set_window_size(1235, 693)
# 输入搜索信息
self.driver.find_element(By.ID, "query").click()
self.driver.find_element(By.ID, "query").send_keys("霍格沃兹测试开发")
# 点击搜索
self.driver.find_element(By.ID, "stb").click()
element = self.driver.find_element(By.ID, "stb")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
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 TestDemo01():
def setup_method(self, method):
# 实例化chromedriver
self.driver = webdriver.Chrome()
# 添加全局隐式等待
self.driver.implicitly_wait(5)
def teardown_method(self, method):
# 关闭driver
self.driver.quit()
def test_demo01(self):
# 访问网站
self.driver.get("https://www.baidu.com/")
# 设置窗口
self.driver.set_window_size(1330, 718)
# 点击输入框
self.driver.find_element(By.ID, "kw").click()
# 输入框输入信息
self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试开发")
# 点击搜索按钮
self.driver.find_element(By.ID, "su").click()
# 等待界面加载
time.sleep(5)
# 元素定位后获取文本信息
res = self.driver.find_element(By.XPATH,"//*[@id='1']/h3/a").get_attribute("text")
# 打印文本信息
print(res)
# 添加断言
assert "霍格沃兹测试开发" in res
# 查看界面展示
time.sleep(5)
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
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.Dimension;
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 TestSogouTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void testSogou() {
driver.get("https://www.sogou.com/");
driver.manage().window().setSize(new Dimension(1671, 1417));
driver.findElement(By.id("query")).sendKeys("霍格沃兹测试开发");
driver.findElement(By.id("query")).sendKeys(Keys.ENTER);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>beginnerartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>11java.version>
<maven.compiler.target>11maven.compiler.target>
<junit.jupiter.version>5.8.2junit.jupiter.version>
<maven.compiler.version>3.8.1maven.compiler.version>
<maven.surefire.version>3.0.0-M5maven.surefire.version>
<hamcrest.version>2.2hamcrest.version>
<maven-surefire-plugin.version>3.0.0-M5maven-surefire-plugin.version>
<slf4j.version>2.0.0-alpha7slf4j.version>
<logback.version>1.3.0-alpha16logback.version>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>${logback.version}version>
dependency>
<dependency>
<groupId>org.seleniumhq.seleniumgroupId>
<artifactId>selenium-javaartifactId>
<version>4.2.1version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<version>${junit.jupiter.version}version>
dependency>
<dependency>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
<version>${junit.jupiter.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<parameters>trueparameters>
<source>11source>
<target>11target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>3.0.0-M7version>
<configuration>
<includes>
<include>**/*Test.javainclude>
includes>
configuration>
plugin>
plugins>
build>
project>
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
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.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.time.Duration;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
import static org.junit.Assert.assertEquals;
public class TestSogouTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
// 实例化chromedriver
driver = new ChromeDriver();
// 添加全局隐式等待
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@After
// 关闭driver
public void tearDown() {
driver.quit();
}
@Test
public void testSogou() {
// 打开网页
driver.get("https://www.sogou.com/");
// 设置窗口
driver.manage().window().setSize(new Dimension(1671, 1417));
// 输入霍格沃兹测试开发
driver.findElement(By.id("query")).sendKeys("霍格沃兹测试开发");
// 回车搜索
driver.findElement(By.id("query")).sendKeys(Keys.ENTER);
// 获取搜索的文本结果
String text = driver.findElement(By.cssSelector("#sogou_vr_30000000_0 > em")).getText();
// 断言是否包含期望文本
assertEquals("霍格沃兹测试开发", text);
}
}