2014-02-10 3 views
-1

현재 셀레늄 WebDriver, JavaTestNG 프레임 작업.Selenium WebDriver를 사용하여 testNG에서 테스트를 실행하는 방법은 무엇입니까?

TestNG 프레임 워크에 대한 아이디어를 제공하십시오.

  • 예를 들어, 내가 파일 test.java 있습니다. @BeforeTest, @Test, @AfterTest으로 Java 코드를 작성했습니다. 각 테스트가 실행되고 얼마나 많은 테스트가 통과되었고 얼마나 많은 테스트가 실패했는지 코드를 실행하는 중입니다.

  • 하지만 난이 secnario에 대한 솔루션을 원하는 :

  • 을 나는 두 개의 탭이 호출이 기본 및 외부

enter image description here

  • 대 내부 기본 탭을 클릭 한 후 그 특정 탭에 대한 많은 테스트를 실행하고 싶다면 일단 테스트가 실행되면 필요합니다. Internal vs External을 클릭하면 모든 테스트가 해당 탭에 속하게됩니다.

  • 어떻게 TestNG 프레임 작업에서 결과를 얻을 수 있습니까?

다음 코드 :

public class OverviewAndEvolutionPR{ 
private static Logger Log = Logger.getLogger(OverviewAndEvolutionPR.class.getName()); 
private WebDriver driver; 
private StringBuffer verificationErrors = new StringBuffer(); 
Properties p= new Properties(); 
public Selenium selenium; 
//@BeforeMethod 
@BeforeTest 
public void Login() throws Exception { 
driver = new FirefoxDriver(); 
try { 
p.load(new FileInputStream("C:/Login.txt")); 
} catch (Exception e) { 
e.getMessage(); 
} 
String url=p.getProperty("url"); 
DOMConfigurator.configure("src/log4j.xml"); 
Log.info("______________________________________________________________"); 
Log.info("Initializing Selenium..."); 
selenium = new DefaultSelenium("localhost", 4444, "*firefox",url); 
Thread.sleep(5000); 
Log.info("Selenium instance started"); 
try { 
p.load(new FileInputStream("C:/Login.txt")); 
} catch (Exception e) { 
e.getMessage(); 
} 
Log.info("Accessing Stored uid,pwd from the stored text file"); 
String uid=p.getProperty("loginUsername"); 
String pwd=p.getProperty("loginPassword"); 
Log.info("Retrieved uid pwd from the text file"); 
try 
{ 
driver.get("https://test.com");//example i had given like this 
} 
catch(Exception e) 
{ 
Reporter.log("network server is slow..check internet connection"); 
Log.info("Unable to open the website"); 
throw new Error("network server is slow..check internet connection"); 
} 
performLogin(uid,pwd); 
} 
public void performLogin(String uid,String pwd) throws Exception 
{ 
Log.info("Sign in to the OneReports website"); 
Thread.sleep(5000); 
Log.info("Enter Username"); 
driver.findElement(By.id("loginUsername")).sendKeys(uid); 
Log.info("Enter Password"); 
driver.findElement(By.id("loginPassword")).sendKeys(pwd); 
//submit 
Log.info("Submitting login details"); 
waitforElement(driver,120 , "//*[@id='submit']"); 
driver.findElement(By.id("submit")).submit(); 
Thread.sleep(6000); 
Actions actions = new Actions(driver); 
Log.info("Clicking on Reports link"); 
if(existsElement("reports")==true){ 
WebElement menuHoverLink = driver.findElement(By.id("reports")); 
actions.moveToElement(menuHoverLink).perform(); 
Thread.sleep(6000); 
} 
else{ 
Log.info("element not present"); 
System.out.println("element not present -- so it entered the else loop"); 
} 
Log.info("Clicking on Extranet link"); 
if(existsElement("extranet")==true){ 
WebElement menuHoverLink = driver.findElement(By.id("extranet")); 
actions.moveToElement(menuHoverLink).perform(); 
Thread.sleep(6000); 
} 
else{ 
Log.info("element not present"); 
System.out.println("element not present -- so it entered the else loop"); 
} 
Log.info("Clicking on PR link"); 
if(existsElement("ext-pr")==true){ 
WebElement menuHoverLink = driver.findElement(By.id("ext-pr")); 
actions.moveToElement(menuHoverLink).perform(); 
Thread.sleep(6000); 
} 
else{ 
Log.info("element not present"); 
System.out.println("element not present -- so it entered the else loop"); 
} 
Log.info("Clicking on Overview and Evolution PR link"); 
if(existsElement("ext-pr-backlog-evolution")==true){ 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", driver.findElement(By.id("ext-pr-backlog-evolution"))); 
Thread.sleep(6000); 
} 
else{ 
Log.info("element not present"); 
System.out.println("element not present -- so it entered the else loop"); 
} 
} 
//Filter selection-1 
//This filter selection need to happen in Default tab 
@Test() 
public void Filterselection_1() throws Exception{ 
Log.info("Clicking on Visualization dropdown"); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
    executor.executeScript("document.getElementById('visualizationId').style.display='block';"); 
Select select = new Select(driver.findElement(By.id("visualizationId"))); 
select.selectByVisibleText("Week"); 
Thread.sleep(6000); 
Log.info("Clicking on Period dropdown"); 
JavascriptExecutor executor1 = (JavascriptExecutor)driver; 
executor1.executeScript("document.getElementById('periodId').style.display='block';"); 
Select select1 = new Select(driver.findElement(By.id("periodId"))); 
select1.selectByVisibleText("Last 4 Weeks"); 
Thread.sleep(6000); 
Log.info("Clicking on Type dropdown"); 
JavascriptExecutor executor2 = (JavascriptExecutor)driver; 
executor2.executeScript("document.getElementById('classificationId').style.display='block';"); 
Select select2 = new Select(driver.findElement(By.id("classificationId"))); 
select2.selectByVisibleText("Customer PRs"); 
Thread.sleep(6000); 
Log.info("Clicking on Apply Filter button"); 
driver.findElement(By.id("kpiFilterSubmit")).click(); 
} 
//In the default tab many filter section i will have once it completed then i need to move to other tab and need to check the filter selection 
//Filter selection-2 
//It need to happen in the Internal vs External tab 
@Test 
public void Filterselection_2() throws Exception{ 
Log.info("Clicking Internal Vs External tab"); 
driver.findElement(By.linkText("Internal vs External")).click(); 
Thread.sleep(6000); 
Log.info("Clicking on Visualization dropdown"); 
JavascriptExecutor executor3 = (JavascriptExecutor)driver; 
executor3.executeScript("document.getElementById('visualizationId').style.display='block';"); 
Select select3 = new Select(driver.findElement(By.id("visualizationId"))); 
select3.selectByVisibleText("ICC"); 
Thread.sleep(6000); 
Log.info("Clicking on Type dropdown"); 
JavascriptExecutor executor02 = (JavascriptExecutor)driver; 
executor02.executeScript("document.getElementById('classificationId').style.display='block';"); 
Select select02 = new Select(driver.findElement(By.id("classificationId"))); 
select02.selectByVisibleText("Internal PRs"); 
Thread.sleep(6000); 
Log.info("Clicking on topography dropdown"); 
JavascriptExecutor executor4= (JavascriptExecutor)driver; 
executor4.executeScript("document.getElementById('topographyId').style.display='block';"); 
Select select4 = new Select(driver.findElement(By.id("topographyId"))); 
select4.selectByVisibleText("ICC"); 
Thread.sleep(6000); 
Log.info("Clicking on Apply Filter button"); 
driver.findElement(By.id("kpiFilterSubmit")).click(); 
Thread.sleep(6000); 
} 
private boolean existsElement(String id) { 
try { 
driver.findElement(By.id(id)); 
} catch (Exception e) { 
System.out.println("id is not present "); 
return false; 
} 
return true; 
} 
private void waitforElement(WebDriver driver2, int i, String string) { 
// TODO Auto-generated method stub 

} 
//@AfterMethod 
@AfterTest 
public void tearDown() throws Exception { 
Log.info("Stopping Selenium..."); 
Log.info("______________________________________________________________"); 
driver.quit(); 
String verificationErrorString = verificationErrors.toString(); 
if (!"".equals(verificationErrorString)) { 
Assert.fail(verificationErrorString); 
} 
} 
}  
+0

어떤 하나의 시나리오 – Amirdha

+0

u는 u를 실행 ... 모두 탭에 대한 테스트의 동일한 세트를 필요로 의미합니까 저를 도와주세요 ??? – Anuragh27crony

답변

0

이 테스트를 sequentialize하는 속성을 @Test의 dependsOnMethods을 사용합니다. TestNG의 특수 효과에 대한 자세한 내용은 TectNG Annotations에서 확인할 수 있습니다.

+0

예. 두 탭에 대해 동일한 테스트 세트를 실행하려고합니다. 첫 번째 탭 하나를 완료해야 다음 탭을 실행해야합니다. – Amirdha

+0

탭이있는 모든 WebElements를 가져온 후 간단한 for 루프를 사용할 수 있습니다. 또 다른 방법은 @dataprovider를 사용하는 것입니다.** dataprovider **는 xls에서 입력을 읽고 xls의 각 행에 대한 모든 테스트를 실행합니다. 따라서 각 탭을 dataprovider xls의 행으로 제공 할 수 있습니다. 내가 제공 한 링크를 살펴 본다면, 내가 가장 잘 맞는 해결책을 찾을 수있을 것이라고 확신합니다. – Husam

0

그렇다면이 테스트를 추상화하고 "기본값"및 "내부 대 외부"에서 호출하지 않는 이유는 무엇입니까? 당신이 사용하는 경우 Page Objects

+0

한 번만 위의 추가 된 코드를 확인하는 데 도움이 될 수 있습니다. – Amirdha

0

TestNG에서 그룹 주석을 사용할 수 있습니다. 기본 탭에서 모든 필요한 테스트 케이스를 작성 후

public class Test1 { 
    @Test(groups = { "functest", "checkintest" }) 
    public void testMethod1() { 
    } 

    @Test(groups = {"functest", "checkintest"}) 
    public void testMethod2() { 
    } 

    @Test(groups = { "functest" }) 
    public void testMethod3() { 
    } 
} 

다음과 같이 하나 개의 그룹으로 그 테스트 케이스를 추가하고 그룹 테스트 케이스가 완료되면 그것은, 그것은 또 다른를 실행합니다 특정 그룹 테스트 케이스를 실행합니다.

testng.xml

<test name="Test1"> 
    <groups> 
    <run> 
     <include name="functest"/> 
    </run> 
    </groups> 
    <classes> 
    <class name="example1.Test1"/> 
    </classes> 
</test> 
관련 문제