create account

[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR] by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 · (edited)
$14.12
[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]
![redsjavahouse1591357_1280.jpg](https://steemitimages.com/0x0/https://files.steempeak.com/file/steempeak/june0620/DKldjbG3-reds-java-house-1591357_1280.jpg)
image source: [pixabay](https://pixabay.com/)

---

지난 시간에 잠시 언급했던 TestNG annotation 중 `@Test` annotation의 속성을 좀 더 알아본다. `@Test` annotation은 테스트하려는 하나의 케이스를 표현한다. 이를테면 로그인, 글쓰기 등을 하나의 케이스로 볼 수 있겠다. 이런 케이스 메소드 위에 `@Test`를 넣어 케이스를 쉽게 관리할 수 있고 속성을 추가하여 부가적인 기능도 추가할 수 있다.

아래 예제를 한번 보자.

1. 총 세 개의 케이스를 지정한다.
2. `enabled` 속성으로 해당 케이스를 실행할지 안 할지 설정한다. 디폴트 값은 true이고 false일 경우 이 케이스를 건너뛴다. `@Test`앞에 주석(//)만 추가하면 동일 기능인데 무슨 차이가 있는지, 왜 사용해야 하는지는 잘 모르겠다.
3. `description`은 말 그대로 이 케이스의 설명이고, 상세하게 기입할수록 좋을 것 같다.
4. `timeOut`는 이 케이스가 실행되는 기대 시간이다. 시간을 초과하면 Exception이 발생하고 리포트에 Fail로 기록된다. UI 자동화에서는 큰 효과가 있을지 의문이지만, 속도를 중요시하는 API 테스트일 경우 상당한 효과가 있을 것으로 보인다.

```
    @Test(enabled = false, description = "글 목록을 불러온다.")
    public void CASE_01_GetPosts() {
        .... Some Action ...
    }

    @Test(timeOut = 60000, description = "로그인 한다.")
    public void CASE_02_Login() {
        .... Some Action ...
    }

    @Test(description = "스팀잇에 글 쓰기 한다. ")
    public void CASE_03_Write() {
        .... Some Action ...
    }
```

위 예제에서 메소드 명에 번호가 추가되어 있는데 이는 TestNG가 테스트를 수행할 때 순서를 정하기 위함이다. TestNG는 메소드 name ascending 순으로 테스트를 수행한다. 하지만 메소드에 꼭 번호를 매겨야 하는 건 아니다. 이 또한 속성으로 대신할 수 있는데 priority 속성으로 순서를 정할 수 있다.

```
    @Test(priority = 3, description = "스팀잇에 글 쓰기 한다. ")
    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 = "로그인 한다.")
    public void CASE_02_Login() throws InterruptedException {
        // Click the login button
        driver.findElement(By.linkText("로그인")).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 = "스팀잇에 글 쓰기 한다. ")
    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
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 65 others
properties (23)
authorjune0620
permlinkjava-web-automation-selenium-with-testng-3-test-annotation-kr
categoryhive-101145
json_metadata{"tags":["hive-101145","kr","mini","steemstem","sct-kr","sct-freeboard","zzan","java","coding","sct","partiko"],"image":["https://files.steempeak.com/file/steempeak/june0620/DKldjbG3-reds-java-house-1591357_1280.jpg"],"links":["https://pixabay.com/"],"app":"partiko","format":"markdown","canonical_url":"https://www.steemcoinpan.com/@june0620/java-web-automation-selenium-with-testng-3-test-annotation-kr"}
created2020-03-14 12:44:18
last_update2020-03-15 12:15:36
depth0
children7
last_payout2020-03-21 12:44:18
cashout_time1969-12-31 23:59:59
total_payout_value7.715 HBD
curator_payout_value6.402 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,632
author_reputation118,592,211,436,406
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,280
net_rshares36,168,263,305,728
author_curate_reward""
vote details (129)
@florianopolis ·
***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, *What does the Bible Say About Tithing?*
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.)***
https://youtu.be/49OGYhhXXKY
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](https://discord.gg/vzHFNd6) 
Join our Official Community: https://steemit.com/created/hive-182074
👍  
👎  
properties (23)
authorflorianopolis
permlinkon3ubytib1j
categoryhive-101145
json_metadata""
created2020-03-14 12:52:33
last_update2020-03-14 12:52:33
depth1
children0
last_payout2020-03-21 12:52:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length692
author_reputation-5,988,767,749,052
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout10,000.000 HBD
percent_hbd100
post_id96,346,426
net_rshares-24,557,712,871
author_curate_reward""
vote details (2)
@teamcn-shop ·
你好鸭,june0620!

@yanhan给您叫了一份外卖!

**巧克力蛋糕** <br> ![](https://steemitimages.com/0x0/https://cdn.steemitimages.com/DQma1eqtjk48SZHV2gjMPrJKVEDrXgYMD9MkyY58YSwP6LT/JPEG_20180525_134924_5872129153971841949.jpg)

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

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

properties (22)
authorteamcn-shop
permlinkwherein-1584190106924
categoryhive-101145
json_metadata"{"app":"teamcn-shop bot/1.0"}"
created2020-03-14 12:48:42
last_update2020-03-14 12:48:42
depth1
children0
last_payout2020-03-21 12:48:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length256
author_reputation11,393,746,055,281
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,346
net_rshares0
@yanhan ·
@tipu curate
!shop

来自于 [WhereIn Android] (http://www.wherein.io)
properties (22)
authoryanhan
permlinkwherein-1584190106924
categoryhive-101145
json_metadata""
created2020-03-14 12:48:30
last_update2020-03-14 12:48:30
depth1
children4
last_payout2020-03-21 12:48:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation323,731,670,852,703
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,343
net_rshares0
@june0620 ·
晚上好啊😀😎
properties (22)
authorjune0620
permlinkre-yanhan-2020314t215011618z
categoryhive-101145
json_metadata{"tags":["esteem"],"app":"esteem/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-03-14 12:50:15
last_update2020-03-14 12:50:15
depth2
children2
last_payout2020-03-21 12:50:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation118,592,211,436,406
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,375
net_rshares0
@annepink ·
我在这呢🤭

Posted using [Partiko Android](https://partiko.app/referral/annepink)
properties (22)
authorannepink
permlinkannepink-re-june0620-re-yanhan-2020314t215011618z-20200314t125131417z
categoryhive-101145
json_metadata{"app":"partiko","client":"android"}
created2020-03-14 12:51:30
last_update2020-03-14 12:51:30
depth3
children1
last_payout2020-03-21 12:51:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length76
author_reputation1,030,853,853,537,931
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,408
net_rshares0
@tipu ·
<a href="https://tipu.online/curator?yanhan" target="_blank">Upvoted  &#128076;</a> (Mana: 5/15 - <a href="https://steempeak.com/steem/@tipu/tipu-curate-project-update-recharging-curation-mana" target="_blank">need recharge</a>?)
properties (22)
authortipu
permlinkre-wherein-1584190106924-20200314t124845
categoryhive-101145
json_metadata""
created2020-03-14 12:48:42
last_update2020-03-14 12:48:42
depth2
children0
last_payout2020-03-21 12:48:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length229
author_reputation55,914,556,170,175
root_title"[JAVA #14] [Web Automation #14] Selenium With TestNG #3 @Test annotation [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,346,350
net_rshares0