2020년 1월 17일 금요일

TestNG의 ReportNG 설정법

pom.xml

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.java.version}</version>
    </dependency>
    <dependency>
        <groupId>org.uncommons</groupId>
        <artifactId>reportng</artifactId>
        <version>${reportng.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>${guice.version}</version>
    </dependency>
    ......
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <properties>
                    <!-- Setting ReportNG listeners -->
                    <property>
                        <name>usedefaultlisteners</name>
                        <value>false</value>
                    </property>
                    <property>
                        <name>listener</name>
                        <value>org.uncommons.reportng.HTMLReporter,
                            org.uncommons.reportng.JUnitXMLReporter</value>
                    </property>
                </properties>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
        ......
    </plugins>
</build>


testng.xml(test-suite)

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="TestNG Suite Name">
    <listeners>
        <listener class-name="org.uncommons.reportng.HTMLReporter" />
        <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
    </listeners>
    <test name="tmon_monthly_maintenance">
        <classes>
            <class name="com.tmoncorp.test.maintenance.TmonHomeTest" />
            <class name="com.tmoncorp.test.maintenance.UserPageTest" />
            .....
        </classes>
    </test>
</suite>

Response Data 추출 시 NullPointerException으로 테스트가 종료되지 않게 하려면

데이터 추출 후 assertion 시 isNotNull로 하려고 했으나 데이터 추출하는 부분에서 NullPointerException이 발생하게 되면 assert를 하지 않고 바로 테스트가 종료된다.

리포트에서도 NullPointerException으로 fail이 발생했다고만 나와서 왜 NullPointerException이 났는지 코드를 봐야 하는 문제점이 있다.

해결책으로는 MapUtils(org.apache.commons.collections)를 사용하면 된다.
MapUtils의 get Method들은 내부적으로 null 체크를 한다.

<response>
{
"data": {
"dealSrl": [
1111111111
],
"total": 1
},
"httpStatus": "OK",
"httpCode": 200
}
"assertion  : data.dealSrl[0]의 dealSrl 값 확인" 이라고 했을 때 

Map<Object, Object> resMap = jsonPath.get("data");
List<Long> dealSrlList = (List) MapUtils.getObject(resMap, "dealSrl");
softAssertions.assertThat(dealSrlList.size()).as(methodName).isEqualTo(total);
 
Long dealSrl = dealSrlList.get(0);
logger.debug("dealSrl : " + dealSrl);
softAssertions.assertThat(dealSrl).as(methodName).isNotNull();

2020년 1월 16일 목요일

.gitignore 내용

#*#
.#*
*~
_site/
*/src/META-INF/
*/src/main/java/META-INF/
bin/
target/
.classpath
.project
.DS_Store
.settings/
.springBeans
*.iml
*.iws
*.ipr
.idea/
code/
cargo-installs/
atlassian-ide-plugin.xml
deploy/

Selenium implicitly wait & explicitly wait

[implicitly wait]
해당 페이지에 속한 element가 모두 로딩될 때 까지 기다리는 방식

syntax
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

example
driver = new ChromeDriver(co);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;


[explicitly wait]
특정 element가 나타날 때 까지 기다려주는 방식

syntax
WebDriverWait wait = new WebDriverWait(WebDriverReference, TimeOut);

exmaple
driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 20);
....
WebElement element;
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(",,,")));
element.click();

ChromeOptions argument

ChromeOptions argument 설명

https://peter.sh/experiments/chromium-command-line-switches/


2020년 1월 14일 화요일

HttpStatusCode 체크

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
 
 
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(new HttpGet(URL));
int statusCode = response.getStatusLine().getStatusCode();
logger.debug("StatusCode: {}", statusCode);

2019년 12월 5일 목요일

Selenium implicitly-wait and explicitly-wait

implicitly-wait : 브라우져 전체 요소가 로드될 때 까지 기다린다.
explicitly-wait : 특정 요소가 로드될 때 까지 기다린다.

implicitly wait
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WaitTest {

 private WebDriver driver;
 private String baseUrl;
 private WebElement element;

 @BeforeMethod
 public void setUp() throws Exception {
  driver = new FirefoxDriver();
  baseUrl = "http://www.google.com";
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }

 @Test
 public void testUntitled() throws Exception {
  driver.get(baseUrl);
  element = driver.findElement(By.id("lst-ib"));
  element.sendKeys("Selenium WebDriver Interview questions");
  element.sendKeys(Keys.RETURN);
  List<WebElement> list = driver.findElements(By.className("_Rm"));
  System.out.println(list.size());
 }

 @AfterMethod
 public void tearDown() throws Exception {
  driver.quit();
 }
}


Explicitly wate
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ExpectedConditionExample {
 // created reference variable for WebDriver
 WebDriver driver;

 @BeforeMethod
 public void setup() throws InterruptedException {
  // initializing driver variable using FirefoxDriver
  driver=new FirefoxDriver();
  // launching gmail.com on the browser
  driver.get("https://gmail.com");
  // maximized the browser window
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @Test
 public void test() throws InterruptedException {
  // saving the GUI element reference into a "element" variable of WebElement type
  WebElement element = driver.findElement(By.id("Email"));
  // entering username
  element.sendKeys("dummy@gmail.com");
  element.sendKeys(Keys.RETURN);
  // entering password
  driver.findElement(By.id("Passwd")).sendKeys("password");
  // clicking signin button
  driver.findElement(By.id("signIn")).click();
  // explicit wait - to wait for the compose button to be click-able
  WebDriverWait wait = new WebDriverWait(driver,30);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
  // click on the compose button as soon as the "compose" button is visible
  driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
 }

 @AfterMethod
 public void teardown() {
  // closes all the browser windows opened by web driver
  driver.quit();
 }
}


Fluent wait
1
2
3
4
5
6
7
8
9
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
 public WebElement applyy(WebDriver driver) {
 return driver.findElement(By.id("foo"));
}});