Software testing/test development丨SeleniumIDE automation use case recording, test case structure analysis

This article is a sharing of learning notes for students of Hogwarts Test Development Society

Original link: https://ceshiren.com/t/topic/24832

1. SeleniumIDE use case recording

SeleniumIDE use case recording usage scenario

  • Just started UI automation testing
  • Poor team code base
  • After the technology grows, the learning value is not high

SeleniumIDE download and installation

  • Official website: https://www.selenium.dev/
  • Chrome plugin: https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd
  • Firefox add-on: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
  • github releases: https://github.com/SeleniumHQ/selenium-ide/releases
  • Other versions: https://addons.mozilla.org/en-GB/firefox/addon/selenium-ide/versions/ Note: Chrome plug-ins cannot be downloaded in China, but Firefox can be downloaded directly.

Startup

  • Once installed, launch it by clicking its icon in your browser’s menu bar:

  • If you don’t see the icon, first make sure that the Selenium IDE extension is installed

  • Access all plugins via the link below

    • Chrome: chrome://extensions
    • Firefox: about:addons

Common functions of SeleniumIDE

image

  1. Create, save, open
  2. Start and stop recording
  3. Run all instances in 8
  4. run a single instance
  5. debug mode
  6. Adjust the running speed of the case
  7. URL to record
  8. instance list
  9. action, target, value
  10. Explanation of a single command
  11. run log

Other common features

  • use case management
  • save and playback

SeleniumIDE script export

  • Java
  • Python

2. Structural analysis of automated test cases

Table of Contents

  • use case structure
  • Recording Use Case Analysis
  • Recording Use Case Optimization

Standard use case structure

  • use case title
  • prerequisite
  • use case steps
  • expected outcome
  • actual results
Use Case Heading Type Preconditions Use Case Steps Expected Result Actual Results
Sogou Search Function Positive Example Enter Sogou Homepage 1. Enter search keywords 2. Press the Enter key 1. The search is successful 2. The search result list contains keywords

Use case structure comparison

automated test case function
Use case title Test package, file, class, method name Use case unique identifier
Precondition setup, setup_class (Pytest);
BeforeEach, BeforeAll (JUnit) Preparatory actions before test cases , such as reading data or driver initialization
Use case steps Code logic in the test method Test case specific step behavior
Expected result assert Actual result = expected result Assert to confirm whether the use case is executed Success
Actual result assert actual result = expected result Assertion, confirm whether the use case is executed successfully
AfterEach teardown, teardown_class (Pytest);
@AfterEach, @ AfterAll (JUnit) Clean up dirty data, close the driver process

IDE recording script

  • Script steps:

    • Visit the Sogou website
    • Type “Hogwarts Test Development” into the search box
    • click the search button
# 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):
    # Open the web page and set the window
    self.driver.get("https://www.sogou.com/")
    self.driver.set_window_size(1235, 693)
    # Enter search information
    self.driver.find_element(By.ID, "query").click()
    self.driver.find_element(By.ID, "query").send_keys("Hogwarts Test Development")
    # Click to search
    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()

image

Script optimization

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):
    # instantiate chromedriver
    self.driver = webdriver.Chrome()
    # Add global implicit wait
    self.driver.implicitly_wait(5)
  
  def teardown_method(self, method):
    # Close the driver
    self. driver. quit()
  
  def test_demo01(self):
    # Visit the website
    self.driver.get("https://www.baidu.com/")
    # set window
    self.driver.set_window_size(1330, 718)
    # Click on the input box
    self.driver.find_element(By.ID, "kw").click()
    # Enter the information in the input box
    self.driver.find_element(By.ID, "kw").send_keys("Hogwarts Test Development")
    # Click the search button
    self.driver.find_element(By.ID, "su").click()
    # Wait for the interface to load
    time. sleep(5)
    # Get the text information after the element is positioned
    res = self.driver.find_element(By.XPATH,"//*[@id='1']/h3/a").get_attribute("text")
    # print text message
    print(res)
    # add assertion
    assert "Hogwarts Test Development" in res
    # View interface display
    time. sleep(5)

IDE recording script (Java)

// 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("Hogwarts Test Development");
    driver.findElement(By.id("query")).sendKeys(Keys.ENTER);
  }
}

pom dependency (Java)

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>beginner</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>11</java.version>
        <!-- To use Java 11 language features ( -source 11 ) and also want compiled classes to be compatible with JVM 11 ( -target 11 ), you can add the following two properties, which are the names of the default properties plugin parameters -->
        <maven.compiler.target>11</maven.compiler.target>
        <!-- Corresponding to the version number of junit Jupiter; if you put it here, you don't need to write the version number in each dependency, which will cause the corresponding version number to conflict -->
        <junit.jupiter.version>5.8.2</junit.jupiter.version>
        <maven.compiler.version>3.8.1</maven.compiler.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
        <hamcrest.version>2.2</hamcrest.version>
        <!-- plugins -->
        <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
        <!-- log log -->
        <slf4j.version>2.0.0-alpha7</slf4j.version>
        <logback.version>1.3.0-alpha16</logback.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
    </dependencies>
    <build>
        <!-- Dependency plugin for maven running -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <parameters>true</parameters>
                    <source>11</source>
                    <target>11</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Script optimization (Java)

  • Implicit waiting (just understand)
  • Assertion information
// 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() {<!-- -->
    // instantiate chromedriver
    driver = new ChromeDriver();
    // add global implicit wait
    js = (JavascriptExecutor) driver;
    vars = new HashMap<String, Object>();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

  }
  @After
  // close the driver
  public void tearDown() {<!-- -->
    driver. quit();
  }
  @Test
  public void testSogou() {<!-- -->
    // open the Web page
    driver.get("https://www.sogou.com/");
    // set window
    driver.manage().window().setSize(new Dimension(1671, 1417));
    // Enter Hogwarts test development
    driver.findElement(By.id("query")).sendKeys("Hogwarts Test Development");
    // Enter to search
    driver.findElement(By.id("query")).sendKeys(Keys.ENTER);
    // Get the text result of the search
    String text = driver.findElement(By.cssSelector("#sogou_vr_30000000_0 > em")).getText();
    // Assert whether the expected text is contained
    assertEquals("Hogwarts Test Development", text);

  }
}