我想在TestNG中制作Test Reoprt。目前正致力于 Selenium WebDriver , Java 和 TestNG 框架工作。
请提供有关 TestNG 框架的建议。
例如,我有一个文件 test.java 。我用java编写过代码 @BeforeTest , @Test , @AfterTest 。在运行代码的同时,每个测试都在运行,并且我得到了多少测试通过以及有多少测试失败。
但我想为secnario提供解决方案:
单击默认选项卡后,我想为该特定选项卡运行多个测试,一旦执行了那些测试,我需要单击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 :(得分:2)
TestNG默认具有报告机制,一旦执行测试,它将自动生成HTML报告到测试输出目录。
您可以从以下路径找到报告
buffered_array[]
boolean flag=true; // either set it to false somewhere in you loop
while(flag){
buffered_array[] =.....; // set to beginning/reload
while (buffered_array.hasNext){
if(condition){
break;
}
}
if(condition){break; or flag=false;} // or break the outter loop with some condition
}
答案 1 :(得分:0)
在第二次测试之前写
@Test(dependsOnMethods="Filterselection_1")
这样,只有在Filterseclection_1结束后才会执行第二次测试,并且不会同时运行测试。默认情况下,TestNG并行运行测试(您可以更改它,但不建议这样做。)
答案 2 :(得分:0)
根据您的要求,您可以使用TestNG组并依赖于注释。 创建两个组 @Test(groups = {" Default Tab"})=为要在默认方案中运行的所有测试写入此内容。 @Test(groups = {" Internal vs External Tab"}):为要在默认方案中运行的所有测试写入此内容。 @Test的Alos(groups = {" Internal vs External Tab"})你必须在@Test注释中使用属性“dependsOnGroups”以及组注释。
OR
或者,您可以在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