[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [CN]

Words
297
Reading
2 min
Listen
Play
6y

redsjavahouse1591357_1280.jpg
image source: pixabay


今天详细解剖一下上期讲过的@Test annotation。
@Test annotation 表示一个测试用例。如登录,撰写等都是个用例。
@Test往往写在这些用例函数的开头,即提高可读性,也助于管理用例,还可加属性添加更多的功能。

让我们看看下列例子吧。

  1. 定义三个用例。
  2. enabled 属性可以设置执行后者不执行该用例,false即可忽略用例。默认为true。
  3. description可以加描述,越详细越好。
  4. timeOut可以给函数定时,超时即抛出 Exception 并记录到报告。此属性比起UI自动化,更广泛用于API自动化测试上。
    @Test(enabled = false, description = "获取帖子列表。")
    public void CASE_01_GetPosts() {
        .... Some Action ...
    }

    @Test(timeOut = 60000, description = "登录到 steemit。")
    public void CASE_02_Login() {
        .... Some Action ...
    }

    @Test(description = "撰写 Steemit 文章。")
    public void CASE_03_Write() {
        .... Some Action ...
    }

上述例子的函数名都设置了编号,以便运行时按顺序运行。
但这不是唯一的方法,还有一个方法可以设置测试顺序,就是用 priority 属性。
如下:

    @Test(priority = 3, description = "撰写 Steemit 文章。")
    public void Write() {
        .... Some Action ...
    }

除此之外,还支持很多属性,如dependsOnMethods, groups, dataProvider,dataProviderClass
往后再自习观察相关属性吧。
今天到此为止~

package com.steem.webatuo;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Steemit {
    WebDriver driver;

    @BeforeClass
    public void SetUp() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.get("STEEMIT URL");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    @Test(enabled = false, description = "获取帖子列表。")
    public void CASE_01_GetPosts() {
        List<WebElement> list = driver.findElements(By.xpath("//div[@id='posts_list']/ul/li"));
        assertTrue(!list.isEmpty(), "글 목록이 있는지 확인");
        for (WebElement post : list) {
            String title = post.findElement(By.xpath("descendant::h2/a")).getText();
            String author = post.findElement(By.xpath("descendant::span[@class='user__name']")).getText();
            System.out.println(author + "님의 글: " + title);
        }
    }

    @Test(timeOut = 60000, description = "登录到 steemit。")
    public void CASE_02_Login() throws InterruptedException {
        // Click the login button
        driver.findElement(By.linkText("login")).click();
        // type id
        driver.findElement(By.name("username")).sendKeys("june0620");
        // type pw and submit
        WebElement pw = driver.findElement(By.name("password"));
        assertNotNull(pw, "确认密码元素是否显示。");
        pw.sendKeys(Password.getPw());
        pw.submit();
        Thread.sleep(5000);
    }

    @Test(description = "撰写 Steemit 文章。")
    public void CASE_03_Write() throws InterruptedException {
        // Click the write Button
        List<WebElement> writeBtns = driver.findElements(By.xpath("//a[@href='/submit.html']"));
        assertEquals(writeBtns.size(), 1, "确认撰写按钮是否显示。");
        for (WebElement writeBtn : writeBtns) {
            if (writeBtn.isDisplayed()) {
                writeBtn.click();
                Thread.sleep(2000);
                // type Text and Keys
                WebElement editor = driver.findElement(By.xpath("//textarea[@name='body']"));
                String keys = Keys.chord(Keys.LEFT_SHIFT, Keys.ENTER);
                editor.sendKeys("hello!! world", keys, "hello!!! STEEMIT", Keys.ENTER, "안녕, 스팀잇", Keys.ENTER, "你好!似提姆");
                break;
            }
        }
        Thread.sleep(5000);
    }

    @AfterClass
    public void tearDownClass() {
        driver.quit();
    }
}

.
.
.
.
[Cookie 😅]
Seleniun java lib version: 3.141.59
java version: 13.0.1

[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annot... | Ecency