Selenium 的组件神器Selenium IDE

前段时间介绍过UI自动化后起之秀的playwright录制脚本,那么作为UI自动化的前辈的Selenium肯定也应该具有录制脚本的功能,今天它来了就是Selenium的三大组件之一的Selenium IDE。

步骤 1: 使用 Selenium IDE 录制测试

  1. 安装 Selenium IDE:首先,需要在你的浏览器上安装 Selenium IDE。对于 Chrome 或 Firefox,可以从相应的扩展/插件商店下载并安装。

    下载插件地址:国内下载地址:https://www.crx4chrome.com/crx/77585/

  2. 录制测试

  • 打开 Selenium IDE。

  • 创建一个新的项目,命名为 “百度测试”。

  • 在浏览器点击Selenium IDE ,创建一个测试项目,并命名

4ace0bbda1d2577f7f1a1016e1542665.png

  • 如果选择第一个,创建好项目后就会直接让你输入地址

e4f2d00f6b3d4eec3e1cf63bfcada298.png

  • 如果选择创建一个项目,不立刻录制的话就进入项目里面点击 '录制' 按钮,打开百度网站(https://www.baidu.com)。

a92b3d7c6f7f8542197b9b8ab9b1a410.png

  • 在搜索框中输入“测试”,然后点击“百度一下”按钮。

  • 记录下搜索结果页面,然后返回插件页面停止录制。

步骤 2: 导出 Python 脚本

  1. 导出脚本

  • 在 Selenium IDE 中,选择你刚才录制的测试用例。

  • 点击 “文件”> “导出” > “选择 Python(pytest)”。

  • 保存生成的 Python 脚本文件。

Python 脚本示例:导出的 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 TestBaidutest():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_baidutest(self):
    self.driver.get("https://www.baidu.com/")
    self.driver.set_window_size(1050, 652)
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()
    self.driver.find_element(By.ID, "kw").click()
    self.driver.find_element(By.ID, "kw").send_keys("selenium")
    self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
    element = self.driver.find_element(By.CSS_SELECTOR, "section:nth-child(1) section:nth-child(3) .cover-img-boder_1-OG1")
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()

步骤 3: 导出 Java 脚本并使用 TestNG

  1. 导出 Java 脚本

  • 重复上述 Python 脚本的导出步骤,但选择 “Java Junit)” 作为导出选项。

    // 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 BaidutestTest {
      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 baidutest() {
        driver.get("https://www.baidu.com/");
        driver.manage().window().setSize(new Dimension(1050, 652));
        driver.findElement(By.id("kw")).click();
        driver.findElement(By.cssSelector(".bdsug-s")).click();
        {
          WebElement element = driver.findElement(By.cssSelector(".weather-icon"));
          Actions builder = new Actions(driver);
          builder.moveToElement(element).perform();
        }
        driver.findElement(By.id("kw")).click();
        driver.findElement(By.id("kw")).sendKeys("selenium");
        driver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
        {
          WebElement element = driver.findElement(By.cssSelector("section:nth-child(1) section:nth-child(3) .cover-img-boder_1-OG1"));
          Actions builder = new Actions(driver);
          builder.moveToElement(element).perform();
        }
      }
    }

修改为 TestNG

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BaiduTest {
    private WebDriver driver;

    @BeforeTest
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testSearch() {
        driver.get("http://www.baidu.com");
        WebElement element = driver.findElement(By.name("wd"));
        element.sendKeys("测试");
        element.submit();
        Assert.assertTrue(driver.getTitle().contains("测试"));
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}
  • 如果不想用Junit可以将 JUnit 相关的代码部分改写为使用 TestNG。

  • 示例代码可能如下:

配置 Maven 的 pom.xml

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>
  • 为了使用 TestNG 和 Selenium,需要在 Maven 的 pom.xml 文件中添加相应的依赖:

本次分享到此结束,录制完成剩下的也就是修修改改,删删减减,这也节省了许多时间,有什么问题大家可以一起交流

77e7201eccc1ee34489dc5d3a8784b51.jpeg

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是阿尘呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值