首页 文章

如何使用Selenium WebDriver在testNG中运行测试?

提问于
浏览
-1

目前正致力于 Selenium WebDriverJavaTestNG 框架工作 .

请提供有关 TestNG 框架的想法 .

  • 例如,我有一个文件 test.java . 我用 @BeforeTest@Test@AfterTest 编写了java代码 . 在运行代码时,每个测试都在运行,并且我得到了多少测试通过以及有多少测试失败了 .

  • 但我想要secnario的解决方案:

  • 我有两个名为 DefaultInternal vs External 的标签

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);
}
}
}

3 回答

  • 0

    使用@Test的属性 dependsOnMethods 来对测试进行顺序化 . 您可以在_ TectNG Annotations进一步阅读TestNG中的注释 .

  • 0

    那你为什么不抽象那些测试而不是在"Default"和"Internal vs External"中调用它?在你的情况下,我会使用Page Objects

  • 0

    您可以在TestNG中使用组注释 . 在Default选项卡下写下所有必需的测试用例,然后将测试用例添加到一个组中,如下所示

    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>
    

相关问题