2014-02-11 149 views
-1

我想生產測試Reoprt在TestNG.Currently上硒webdriver的的JavaTestNG的工作框架工作。如何使用Selenium WebDriver獲得TestNG框架的測試報告?

請提供關於TestNG框架的建議。

  • 例如,我有一個文件test.java。我已經寫了 @BeforeTest,@Test,@AfterTest。在運行代碼時,每個測試都在運行,並且正在獲取多少測試通過,有多少次失敗。

  • 但我想對於secnario解決方案:

  • 我有兩個選項卡稱爲默認內部VS外部

enter image description here

  • 點擊默認選項卡後我想爲特定的選項卡運行很多測試,一旦那些測試執行,然後我需要點擊內部vs外部,然後我需要運行屬於該選項卡的所有測試。

  • 我如何在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

前2試寫

@Test(dependsOnMethods="Filterselection_1") 

這樣,你將執行Filterseclection_1結束後,才第二次測試,並且測試將不會在運行同時。默認情況下,TestNG並行運行測試(您可以更改它,但不推薦)。

+0

歡迎的StackOverflow!請爲您的答案提供更多的背景信息,而不僅僅是代碼。爲什麼在第二次測試之前,例如? –

2

TestNG具有默認的報告機制,一旦執行測試,它將自動生成HTML報告到test-output目錄。

你可以從下面路徑

your project directory/test-output/index.html 
0

對於您的要求找到一個報告,你可以使用TestNG的組取決於註解。 創建兩個組 @Test(groups = {「Default Tab」})=爲默認情況下要運行的所有測試編寫此代碼。 @Test(groups = {「Internal vs External Tab」}):爲默認情況下要運行的所有測試編寫此代碼。 @Test(groups = {「Internal vs External Tab」})的alos必須在@Test註釋中使用屬性「dependsOnGroups」以及組註釋。

或 或者,您可以在testng.xml文件中指定組依賴項。您可以使用<dependencies>標籤來實現這一目標:

<test name="My suite"> 
    <groups> 
    <dependencies> 
     <group name="Internal vs External Tab" depends-on=" Default Tab" /> 
    </dependencies> 
    </groups> 
</test> 

更多細節: http://testng.org/doc/documentation-main.html#test-groups

相關問題