Java 如何使用SeleniumWebDriver在testNG中运行测试?

Java 如何使用SeleniumWebDriver在testNG中运行测试?,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,目前正在进行Selenium WebDriver、Java和TestNG框架工作 请给出有关TestNG框架的想法 例如,我有一个文件test.java。我已经用java语言编写了java代码 @测试前,@测试,@测试后。在运行代码时,每个测试都在运行,我会得到多少测试通过了,多少测试失败了 但我想为secnario找到解决方案: 我有两个选项卡,分别称为默认值和内部与外部 单击默认选项卡后,我希望为该特定选项卡运行多个测试,一旦执行了这些测试,我需要单击内部与外部,然后我需要运行属于该

目前正在进行Selenium WebDriverJavaTestNG框架工作

请给出有关TestNG框架的想法

  • 例如,我有一个文件test.java。我已经用java语言编写了java代码 @测试前@测试@测试后。在运行代码时,每个测试都在运行,我会得到多少测试通过了,多少测试失败了

  • 但我想为secnario找到解决方案:

  • 我有两个选项卡,分别称为默认值内部与外部

  • 单击默认选项卡后,我希望为该特定选项卡运行多个测试,一旦执行了这些测试,我需要单击内部与外部,然后我需要运行属于该选项卡的所有测试

  • 如何在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);
}
}
}       

使用@Test的属性dependsOnMethods,对测试进行排序。您可以在-。

进一步阅读TestNG中的注释,那么为什么不抽象这些测试,然后在“默认”和“内部与外部”中调用它呢?在您的情况下,我将使用

您可以在TestNG中使用Groups注释。将所有需要的测试用例写在默认选项卡下,然后将这些测试用例添加到一个组中,如下所示

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>


任何人请帮助我了解场景您的意思是您需要为两个选项卡运行相同的测试集吗???是的。我想对这两个选项卡运行相同的测试集,但是。第一个选项卡需要完成,然后下一个选项卡需要运行。在获取所有属于该选项卡的WebElement后,您可以使用simple for循环。另一种方法是使用@dataprovider。dataprovider从xls读取输入,并对xls中的每一行执行所有测试。因此,您可以将每个选项卡作为dataprovider xls中的行提供。如果你浏览我提供的链接,我相信你会找到最适合你的解决方案。任何一次都可以通过查看上面添加的代码来帮助你