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

in SCT.암호화폐.Crypto4 years ago (edited)

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

Sort:  

@tipu curate
今天这么早啊

Posted using Partiko Android

前天错过了,睡着了,所以初始化下发帖时间 😄

睡着了? 做春梦了才睡着的吧🤭

Posted using Partiko Android

很想做春梦,但...
(介绍下女盆友?⁉️🤗)

我觉得最好把cn 标签也加上排前
cn 标签是中文区O神指定的 得到他点赞很不错的

Posted using Partiko Android

我怎么把最重要的给忘了呢😢
多谢提醒😀👍

就拍到这了休息下😅

哎呀~ 拍这么多,好耶~ 哈哈
多谢🙏多谢~~

你那里天气如何?来一份新手村小卖部的美食吧!@teamcn-shop感谢支持。

吃了吗?想一展歌喉吗?好声音@cn-voice欢迎你~感谢支持。

你好鸭,june0620!

@cnbuddy给您叫了一份外卖!

上海人的回忆雪糕

吃饱了吗?跟我猜拳吧! 石头,剪刀,布~

如果您对我的服务满意,请不要吝啬您的点赞~

But the end of all things has drawn near. Therefore be sober-minded and be sober unto prayers.(1 Peter 4:7)

Question from the Bible, Why are some children born with disabilities?

Watch the Video below to know the Answer...
(Sorry for sending this comment. We are not looking for our self profit, our intentions is to preach the words of God in any means possible.)


Comment what you understand of our Youtube Video to receive our full votes. We have 30,000 #SteemPower. It's our little way to Thank you, our beloved friend.
Check our Discord Chat
Join our Official Community: https://steemit.com/created/hive-182074

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.034
BTC 63721.15
ETH 3305.01
USDT 1.00
SBD 3.91