create account

[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR] by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 ·
$8.34
[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]
![redsjavahouse1591357_1280.jpg](https://files.steempeak.com/file/steempeak/june0620/DKldjbG3-reds-java-house-1591357_1280.jpg)
image source: [pixabay](https://pixabay.com/)
***
자동화 테스트에서 가장 중요한 것은 기대결과가 예상대로 노출되었는지 확인하는 것이다. 
예를 들면 글 목록이 정상적으로 노출되는지, 목록의 글 제목, 저자명, 명성, 글 내용, 저자 썸네일 등이 노출되는지를 모두 체크해야 한다. 
또한 어떤 태그가 노출되지 않았는지, 무슨 이유로 노출되지 않았는지 정확하게 기록하는 것도 필요하다.
TestNG Asserting은 바로 이러한 것을 처리하기 위해 존재하며 주로 아래와 같은 asserting이 가장 많이 사용된다. (적어도 내 코드에는 자주 나타난다.)

```
    // object가 null이 아닌지 체크, fail 발생했을 경우 리포트에 message 추가 
    Assert.assertNotNull(object, message);
    // condition이 True인지 체크, fail 발생했을 경우 리포트에 message 추가
    Assert.assertTrue(condition, message);
    // Assert.assertTrue와 반대의 개념
    Assert.assertFalse(condition, message);
    //actual과 expected가 똑같은지 체크(타입도 체크함), fail 발생했을 경우 리포트에 message 추가
    Assert.assertEquals(actual, expected, message);
```

👆 먼저 `import org.testng.Assert.*;` 라이브러리 import 후 코드는 위처럼 작성하면 된다. static 으로 import 하면 코드에 Assert.는 안적어도 된다. 👇
```
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

    ...
    ...

    assertNotNull(object, message); 
    assertTrue(condition, message);
    assertFalse(condition, message);
    assertEquals(actual, expected, message);
```

👇 아래 코드는 1)글 목록이 노출되었는지 2)비밀번호 태그를 불러왔는지 3)글쓰기 버튼이 노출되는지를  체크한다. 
3번은 일부러 fail이 발생하게 체크했다. 
스팀잇 페이지 dom에 글쓰기 버튼이 하나가 있는데 일부러 두개 있는지 체크하여 fail을 발생시킨다. 
```
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
	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
	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
	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(), 2, "글쓰기 버튼이 노출되는지 확인");
		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();
	}
}
```
실행 해 보면 아래와 같이 fail 이 발생하고 리포트에 정확하게 내가 작성한 메시지가 찍히는 것을 볼 수 있다.
![image.png](https://files.steempeak.com/file/steempeak/june0620/dtKozYeX-image.png)
<sub> **만약 리포트에 한글이 깨질 경우 Eclipse가 설치된 경로에서 eclipse.ini 파일 맨 아래에 `-Dfile.encoding=UTF-8`를 넣으면 해결된다. 보통 경로는 `C:\Users\{사용자 컴퓨터 이름}\eclipse`이다.** 👇</sub>
![image.png](https://files.steempeak.com/file/steempeak/june0620/eg63R0EO-image.png)

추가로 아래와 같은 메소드도 지원하지만 자주 다뤄보지 않아서 잘 모르겠다. 나중에 좀 친해져 보도록 하고 오늘은 이만 자즈~~아👇
```
    assertEqualsDeep(actual, expected);
    assertEqualsNoOrder(actual, expected);
    assertNotEquals(actual, expected);
    assertNotEqualsDeep(actual, expected);
    assertSame(actual, expected);
    assertNotSame(actual, expected);
    assertNull(object, message);
    assertThrows(throwableClass, runnable);
```
.
.
.
.
[Cookie 😅]
Seleniun java lib version: 3.141.59 
java version: 13.0.1
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 78 others
properties (23)
authorjune0620
permlinkjava-13-web-automation-13-selenium-with-testng-2-asserting-kr
categoryhive-101145
json_metadata{"tags":["hive-101145","sct-kr","sct-freeboard","kr","mini","steemstem","zzan","palnet","dblog","sct"],"image":["https://files.steempeak.com/file/steempeak/june0620/DKldjbG3-reds-java-house-1591357_1280.jpg","https://files.steempeak.com/file/steempeak/june0620/dtKozYeX-image.png","https://files.steempeak.com/file/steempeak/june0620/eg63R0EO-image.png"],"links":["https://pixabay.com/"],"app":"steemcoinpan/0.1","format":"markdown","canonical_url":"https://www.steemcoinpan.com/@june0620/java-13-web-automation-13-selenium-with-testng-2-asserting-kr"}
created2020-03-10 13:12:42
last_update2020-03-10 13:12:42
depth0
children13
last_payout2020-03-17 13:12:42
cashout_time1969-12-31 23:59:59
total_payout_value4.675 HBD
curator_payout_value3.664 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,815
author_reputation118,592,211,436,406
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,019
net_rshares34,686,296,683,441
author_curate_reward""
vote details (142)
@annepink ·
Hi🙋
@tipu curate 2
!shop

Posted using [Partiko Android](https://partiko.app/referral/annepink)
properties (22)
authorannepink
permlinkannepink-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t131645972z
categoryhive-101145
json_metadata{"app":"partiko","client":"android"}
created2020-03-10 13:16:45
last_update2020-03-10 13:16:45
depth1
children3
last_payout2020-03-17 13:16:45
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_length95
author_reputation778,727,369,935,609
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,144
net_rshares0
@june0620 ·
晚上哈喽啊 😁 
多谢萍萍无私的奉献 哈哈
properties (22)
authorjune0620
permlinkre-annepink-2020310t2221929z
categoryhive-101145
json_metadata{"tags":["esteem"],"app":"esteem/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-03-10 13:21:09
last_update2020-03-10 13:21:09
depth2
children1
last_payout2020-03-17 13:21:09
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_length21
author_reputation118,592,211,436,406
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,255
net_rshares0
@annepink ·
我没奉献神马呀😅

Posted using [Partiko Android](https://partiko.app/referral/annepink)
properties (22)
authorannepink
permlinkannepink-re-june0620-re-annepink-2020310t2221929z-20200310t145345130z
categoryhive-101145
json_metadata{"app":"partiko","client":"android"}
created2020-03-10 14:53:45
last_update2020-03-10 14:53:45
depth3
children0
last_payout2020-03-17 14:53:45
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_length79
author_reputation778,727,369,935,609
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,236,673
net_rshares0
@tipu ·
<a href="https://tipu.online/curator?annepink" target="_blank">Upvoted  &#128076;</a> (Mana: 10/20 - <a href="https://steempeak.com/steem/@tipu/tipu-curate-project-update-recharging-curation-mana" target="_blank">need recharge</a>?)
properties (22)
authortipu
permlinkre-annepink-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t131645972z-20200310t131709
categoryhive-101145
json_metadata""
created2020-03-10 13:17:03
last_update2020-03-10 13:17:03
depth2
children0
last_payout2020-03-17 13:17:03
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_length232
author_reputation55,186,223,023,548
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,154
net_rshares0
@olaf123 ·
### According to the Bible, *The Book of Judas: Should You Believe It?*
### 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/cozSKLCYYwQ
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)
authorolaf123
permlinkeq6tm5zztq
categoryhive-101145
json_metadata""
created2020-03-10 13:20:57
last_update2020-03-10 13:20:57
depth1
children0
last_payout2020-03-17 13:20:57
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_length585
author_reputation-10,813,981,844,129
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout10,000.000 HBD
percent_hbd100
post_id96,234,249
net_rshares-24,817,924,703
author_curate_reward""
vote details (2)
@ravenkim ·
@tipu curate
properties (22)
authorravenkim
permlinkre-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t131641007z
categoryhive-101145
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["hive-101145"],"users":["tipu"],"links":["/@tipu"],"image":[]}
created2020-03-10 13:16:42
last_update2020-03-10 13:16:42
depth1
children6
last_payout2020-03-17 13:16: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_length12
author_reputation107,302,486,367,072
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,140
net_rshares0
@annepink ·
擦 算你的还是算我的呢😂

Posted using [Partiko Android](https://partiko.app/referral/annepink)
properties (22)
authorannepink
permlinkannepink-re-ravenkim-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t132027454z
categoryhive-101145
json_metadata{"app":"partiko","client":"android"}
created2020-03-10 13:20:27
last_update2020-03-10 13:20:27
depth2
children3
last_payout2020-03-17 13:20:27
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_length83
author_reputation778,727,369,935,609
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,243
net_rshares0
@june0620 ·
应该是算你的了 😁😀
properties (22)
authorjune0620
permlinkre-annepink-2020310t22224745z
categoryhive-101145
json_metadata{"tags":["esteem"],"app":"esteem/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-03-10 13:22:48
last_update2020-03-10 13:22:48
depth3
children1
last_payout2020-03-17 13:22:48
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_length10
author_reputation118,592,211,436,406
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,295
net_rshares0
@ravenkim ·
?
properties (22)
authorravenkim
permlinkre-annepink-annepink-re-ravenkim-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t134500418z
categoryhive-101145
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["hive-101145"],"users":[],"links":[],"image":[]}
created2020-03-10 13:45:03
last_update2020-03-10 13:45:03
depth3
children0
last_payout2020-03-17 13:45:03
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_length1
author_reputation107,302,486,367,072
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,836
net_rshares0
@june0620 ·
감사합니다^^
properties (22)
authorjune0620
permlinkre-ravenkim-2020310t222218717z
categoryhive-101145
json_metadata{"tags":["esteem"],"app":"esteem/2.2.4-mobile","format":"markdown+html","community":"hive-125125"}
created2020-03-10 13:22:21
last_update2020-03-10 13:22:21
depth2
children0
last_payout2020-03-17 13:22:21
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_length7
author_reputation118,592,211,436,406
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,284
net_rshares0
@tipu ·
<a href="https://tipu.online/curator?ravenkim" target="_blank">Upvoted  &#128076;</a> (Mana: 15/25 - <a href="https://steempeak.com/steem/@tipu/tipu-curate-project-update-recharging-curation-mana" target="_blank">need recharge</a>?)
properties (22)
authortipu
permlinkre-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t131641007z-20200310t131700
categoryhive-101145
json_metadata""
created2020-03-10 13:16:57
last_update2020-03-10 13:16:57
depth2
children0
last_payout2020-03-17 13:16:57
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_length232
author_reputation55,186,223,023,548
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,148
net_rshares0
@teamcn-shop ·
你好鸭,june0620!

@annepink给您叫了一份外卖!

由 @team-cn 新手村 迎着飓风 骑着村长家后院的鹿 给您送来
**烤肉** <br> ![](https://ipfs.busy.org/ipfs/QmXkA7PZLtjDq5tarH3Z44XKgfuX5NtJ5rfo8nY2RKiFE3)
吃饱了吗?跟我猜拳吧! **石头,剪刀,布~**

如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex
properties (22)
authorteamcn-shop
permlinkannepink-re-june0620-java-13-web-automation-13-selenium-with-testng-2-asserting-kr-20200310t131645972z
categoryhive-101145
json_metadata"{"app":"teamcn-shop bot/1.0"}"
created2020-03-10 13:16:57
last_update2020-03-10 13:16:57
depth1
children0
last_payout2020-03-17 13:16:57
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_length219
author_reputation11,393,746,055,281
root_title"[JAVA #13] [Web Automation #13] Selenium With TestNG #2 Asserting [KR]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id96,234,150
net_rshares0