目录
- capability概述
- capability配置
- Selenium Grid简介
- 分布式运行
capability概述
- Capabilities是WebDriver支持的标准命令之外的扩展命令(配置信息)
- 配置web驱动的属性,如浏览器名称、浏览器平台等。
- 结合Selenium Grid完成分布式、兼容性等测试
- 官网地址: https://www.selenium.dev/zh-cn/documentation/webdriver/capabilities/shared/
capability 使用示例-Python
"""
=====Python版本=====
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_ceshiren():
# 切换成 windows 就会报错
capabilities = {"browserName":"chrome","platformName":"mac"}
# 通过 desired_capabilities 添加配置信息
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.implicitly_wait(5)
driver.get("https://ceshiren.com/")
# 输入框输入搜索内容[霍格沃兹测试学院]
text = driver.find_element(By.CSS_SELECTOR, ".login-button").text
# 点击搜索按钮
print(text)
time.sleep(30)
driver.quit()
capability 使用示例-Java
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
/**
* @Author: 霍格沃兹测试开发学社
* @Desc: '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
*/
public class CapabilityTest {
@Test
void ceshiren(){
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability("platformName", "mac");
chromeOptions.setCapability("browserName", "chrome");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://ceshiren.com/");
String text = driver.findElement(By.cssSelector(".login-button")).getText();
System.out.println(text);
driver.quit();
}
}
Selenium Grid
@startuml
title 分布式
node 主节点 as node1
cloud Linux主机 as linux
cloud Windows主机 as windows
node 火狐浏览器 as firefox
node 其他浏览器 as other
node 谷歌浏览器v101 as 101
node 谷歌浏览器v100 as 100
node1 --> linux
node1 --> windows
linux --> 101
linux --> 100
linux --> firefox
linux --> other
@enduml
-
Selenium Grid 允许我们在多台机器上并行运行测试,并集中管理不同的浏览器版本和浏览器配置(而不是在每个单独的测试中)。
-
官网地址:https://www.selenium.dev/documentation/grid/
演示环境
- 学院搭建的演示环境:https://selenium-node.hogwarts.ceshiren.com/ui#
- (搭建教程会在 docker 章节讲解)
- 保证本地可以正常调通
- 实例化Remote()类并添加相应的配置
- 远程地址
- 设备配置
代码示例-Python
def test_ceshiren2():
hogwarts_grid_url = "https://selenium-node.hogwarts.ceshiren.com/wd/hub"
capabilities = {"browserName":"chrome","browserVersion":"101.0"}
# 配置信息
# 实例化Remote,获取可以远程控制的driver实例对象
# 通过 command_executor 配置selenium hub地址
# 通过 desired_capabilities 添加配置信息
driver = webdriver.Remote(
command_executor=hogwarts_grid_url,
desired_capabilities=capabilities)
driver.implicitly_wait(5)
driver.get("https://ceshiren.com/")
# 输入框输入搜索内容[霍格沃兹测试学院]
text = driver.find_element(By.CSS_SELECTOR, ".login-button").text
# 点击搜索按钮
print(text)
time.sleep(3)
driver.quit()
代码示例-Java
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @Author: 霍格沃兹测试开发学社
* @Desc: '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
*/
public class CapabilityTest {
@Test
void remoteCeshiren() throws MalformedURLException {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability("browserVersion", "101.0");
chromeOptions.setCapability("browserName", "chrome");
WebDriver driver = new RemoteWebDriver(
new URL("https://selenium-node.hogwarts.ceshiren.com/wd/hub"),
chromeOptions);
driver.get("https://ceshiren.com/");
String text = driver.findElement(By.cssSelector(".login-button")).getText();
System.out.println(text);
driver.quit();
}
}
标签:ceshiren,driver,selenium,29,capability,https,text,import,分布式
From: https://www.cnblogs.com/csfsz/p/17934876.html