2014-11-06 3 views
1

Selenium Webdriver를 처음 사용하고이 API를 배우려고합니다. 하나의 테스트에서 여러 어설 션을 효과적으로 사용하는 방법을 알고 싶습니다. 직접적으로 사용하려고 시도했지만 코드 길이가 길어지고 디버깅하기가 매우 어렵습니다. 어떤 제안이라도 끝내는 방법?Java의 webdriver에서 여러 어설 션을 사용하는 방법

package com.eureqa.scripts;  
import java.util.concurrent.TimeUnit;  
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 

    public class Phase1 { 
    public static WebDriver driver; 
    public static WebDriver driver1; 

    public void navigation1(WebDriver driver1) 
    { 
     boolean result=verifyElementPresent(driver1); 
     if(result) 
     { 
      System.out.println("Element found"); 
     } 
     else 
     { 
      System.out.println("Element not found"); 

     } 



     driver1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 



     boolean result1=assertElementPresent(driver1); 
     if(result1) 
     { 
      System.out.println("Element asserted"); 
     } 
     else 
     { 
      System.out.println("Element not asserted"); 
      driver1.quit(); 
     } 

     driver1.findElement(By.linkText("Reports")).click(); 
    } 





    public boolean verifyElementPresent(WebDriver driver1) 
    { 
     try{ 
      driver1.findElement(By.id("commonheader:headerForm:projectlist")); 
      return true; 

     }catch(Exception ex) 
     { 
      return false; 
     } 
    } 

    public boolean assertElementPresent(WebDriver driver1) 
    { 
     try{ 
      driver1.findElement(By.linkText("Reports")); 
      return true; 

     }catch(Exception ex) 
     { 
      return false; 

     } 
    } 




    public static void main(String arr[]) throws InterruptedException 
    { 
     WebDriver driver1=LoginObject.driver(); 
     System.out.println("Object Received"); 

     LoginEureqa m=new LoginEureqa(); 
     m.login(driver1); 

     Phase1 p1=new Phase1(); 
     p1.navigation1(driver1); 
     System.out.println(); 

     System.out.println("Phase1 executed successf`enter code here`ully"); 

    } 

} 
+0

테스트하려는 내용에 대해 자세히 설명해 줄 수 있습니까? 더 나은 코드를 찾고있는 중이거나 지금 가지고있는 것을 실행하려고 할 때 오류가 있습니까? – shri046

+0

다른 자바 파일에서 모든 코드를 사용하여 반복적으로 모든 assertion 메서드를 작성하고 클래스를 가져 와서 필요한 메서드를 사용하여 필요할 때 호출하는 것이 좋습니다. – Subh

+0

내 코드가 이번에는 잘 실행되지만 내 관심사는 여러 번 assaysion 명령을 사용해야합니다. 자바에 별도의 클래스를 생성하고 싶습니다. 모든 유형의 로케이터를 가져올 수있는 메소드를 일반화 할 수 있다면 부울 값을 반환합니다. . 할 수있는 최선의 방법을 제안 해주세요. – Vivek

답변

1

내 셀렌 테스트를 jUnit과 결합하여 jUnit의 어설 션을 사용할 수 있습니다. 그것은 코드를 다소 사용하기 쉽게 만듭니다. 다른 방법으로는 셀렌의 대기 방법을보고 싶을 것입니다.

테스트를 생각할 때 오류 조건이 발견 된 후에도 테스트를 계속 실행해야하는지 여부를 고려해야합니다. 예제에서 verifyElementPresent와 assertElementPresent가 모두 실패한 경우 테스트에서 "Reports"링크를 클릭하는 것이 합리적입니까? 실패 후에도 테스트를 계속 실행할 필요가 없으면 코드가 더 간단 해집니다.

코드를 보면, 아마도 같은 탐색 한 것을 다시 것입니다 : 이제

package com.eureqa.scripts; 

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertFalse; 
import static org.junit.Assert.assertTrue; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class Phase1 { 
    public static WebDriver driver; 
    public static WebDriver driver1; 

    public void navigation1(WebDriver driver1) 
    { 
     WebDriverWait wait = new WebDriverWait(driver, 10); 
     assertTrue("commonheader:headerForm:projectlist not found", 
        driver1.findElements(By.id("commonheader:headerForm:projectlist")).size() == 1); 
     wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Reports"))).click(); 
    } 

    public static void main(String arr[]) throws InterruptedException 
    { 
     WebDriver driver1=LoginObject.driver(); 
     System.out.println("Object Received"); 

     LoginEureqa m=new LoginEureqa(); 
     m.login(driver1); 

     Phase1 p1=new Phase1(); 
     p1.navigation1(driver1); 
     System.out.println(); 

     System.out.println("Phase1 executed successf`enter code here`ully"); 

    } 
} 

을,이 정확히 각 단계에서 실패 때문에, 원래의 게시물에 한 무슨 재현하지 않습니다. 첫 번째 주장이 실패하기를 원하지만 계속하려면 try/catch 블록으로 assertTrue를 감싸고 별도의 메시지를 표시 할 수 있습니다.

navigation1의 코드는 매우 간단합니다. 먼저 ID가 "commonheader : headerForm : projectlist"인 요소를 찾습니다. 해당 요소를 찾지 못하면 "commonheader : headerForm : projectlist not found"메시지와 함께 테스트 오류가 발생합니다.

두 번째 단계는 "보고서"라는 텍스트로 링크를 클릭 할 때까지 기다리는 것입니다. 링크가 10 초 이내에 나타나지 않으면 오류가 발생합니다.

+1

단지 두 가지가 있습니다. '드라이버'가 정적 필드로 초기화되고 있다면 메서드 매개 변수로 전달할 필요가 없습니다. 나는 이것이 단지 제안 된 수정 일 뿐이라는 것을 알고 있지만 JUnit (개인적으로 선호하는)의 경로가 전체 코드를 테스트 케이스와 유사하게 변환 할 수 있습니다. – shri046

관련 문제