【问题标题】:I have total 3 test cases 2 test case in parent class and 1 in child class, when I am running script testcases from first class runs two time我总共有 3 个测试用例,父类中有 2 个测试用例,子类中有 1 个测试用例,当我从头等舱运行脚本测试用例运行两次时
【发布时间】:2019-11-05 13:58:43
【问题描述】:

我有一个包 两个类和总共 3 个测试用例 两个测试用例在父类中,一个在子类中,但是当我从第一个类运行脚本测试用例时运行两次。在开始时以及在第二类测试运行之后再次。我只在父类中启动应用程序

父类

package sanityTest;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
//import org.openqa.selenium.firefox.FirefoxDriver;

public class AdminLogin {

    public static WebDriver driver;
    static Alert alert;
    static String alertmessage = "MenuItem created successfully.";
    static String alertmessage1 = "Menuitem name should be minimum 1 and maximum 150 character.";
    static String alertmessage2 = "Please enter alphanumeric menuitem name with min 1 and max 150 of length.";
    static String alertmessage3 = "Are you sure you want to create recipe?";
    static String alertmessage4 = "Menu item already exists";
    static WebDriverWait wait;



    @Test (priority =1)
    public  void  adminLogin() throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Downloads\\ChromeDriver\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.get("App url");

        Thread.sleep(2000);

        driver.findElement(By.xpath("/html/body/div[3]/p/a[1]/b/span")).click();
        driver.findElement(By.xpath("//*[@id=\"userName\"]")).sendKeys("admin");
        driver.findElement(By.xpath("//*[@id=\"pass\"]")).sendKeys("admin@1234");
        driver.findElement(By.xpath("/html/body/div/div/div/div[2]/form/button[1]")).click();

        String url = driver.getCurrentUrl();

        if ("App url"))
        {
        System.out.println("Admin Logged in successfullly");
        }
        else 
        {
            System.out.println("Login Unsuccessfull");  
        }

        //Selects Partner from Dashboard
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@id=\"bootstrap-data-table\"]/tbody/tr[7]/td[4]/center/button")).click();
        System.out.println("Partner Selected from Dashboard");
    }


    //----------------------------------------------------------------------------------------------------------------------


    @Test (priority = 2)
    public static void createMenuItem()throws Exception
    {

        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[4]/a")).click();
        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[4]/ul/li[2]/a")).click();

        String url1 = driver.getCurrentUrl();
        if (url1.equals("App Url"))
        {
        System.out.println("Create Menuitem form opened successfullly");
        }
        else 
        {
            System.out.println("Create Menuitem form not opened");  
        }




        File file = new File("C:/Users/Desktop/MenuNames.xls");
        Workbook w = Workbook.getWorkbook(file);
        Sheet menulist = w.getSheet("Sheet1");
        int rows = menulist.getRows();
        int columns = menulist.getColumns();
        System.out.println(rows);
        System.out.println(columns);


        String menlistInput [] [] =  new String [rows] [columns];
        for (int i=0; i<rows; i++) {
             for (int j=0; j<columns; j++){

            Cell c = menulist.getCell(j, i);
            menlistInput [i][j] =c.getContents();
            System.out.println(menlistInput [i][j]);
            Pattern pattern = Pattern.compile("['@#_()*]*");

              String str = menlistInput [i][j];
              Matcher matcher = pattern.matcher(str);


        driver.findElement(By.xpath("//*[@id=\"mitems\"]")).sendKeys(menlistInput [i][j]);



                driver.findElement(By.xpath("//*[@id=\"menu\"]")).click();
                driver.findElement(By.xpath("//*[@id=\"menu\"]/option[3]")).click();


        driver.findElement(By.xpath("//*[@id=\"checkbox\"]")).click();



        if (menlistInput [i][j].isEmpty() || matcher.matches() ) {

        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if(alertText.equals(alertmessage1) || alertText.equals(alertmessage2))
        {
            System.out.println("MenuItem Name is blank or contains special characters");
            System.out.println(alertText);
            alert.accept();

            break;
        }
        }





        Alert alert = null;
        try {
            WebDriverWait wait = new WebDriverWait(driver, 5 /*timeout in seconds*/);
            alert = wait.until(ExpectedConditions.alertIsPresent());
        }catch(Exception exp8) { 

        }

        if(alert == null) {
            System.out.println("alert was not present");
            Thread.sleep(3000);
           driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/button[2]")).click();

        }
        else {
            String Recipemessage = alert.getText();
            System.out.println(Recipemessage);

            if (Recipemessage.equals(alertmessage2) || Recipemessage.equals(alertmessage4)) {
                Thread.sleep(3000);
                alert.accept();
                break;
            }
        }


        Thread.sleep(3000);
        Set<String>allowWindow= driver.getWindowHandles();

        ArrayList<String>tabs=new ArrayList <> (allowWindow);

        try {
        driver.switchTo().window(tabs.get(1));
        driver.close();
        driver.switchTo().window(tabs.get(0));
        }
        catch(Exception ex) {

        }




        driver.findElement(By.xpath("//*[@id=\"fetch_data_div\"]/div[2]/center/button"));



        Thread.sleep(2000);

        //Enter Description
        driver.findElement(By.xpath("//*[@id=\"desc\"]")).sendKeys("Mastaniiii");

        //Upload Image      
        driver.findElement(By.xpath("//*[@id=\"menu_img\"]")).sendKeys("C:\\Users\\RSL\\Desktop\\test.jpg");;

        //Click On Submit
        wait = new WebDriverWait(driver, 10);


    boolean invisible = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("pageloaddiv")));

      if(invisible)
      {
          driver.findElement(By.xpath("//*[@id=\"menuitem_form\"]/div[11]/button[1]")).click();
      }


      Thread.sleep(3000);

      alert = driver.switchTo().alert();
     // System.out.println(alert.getText());

      if(alert.getText().equals(alertmessage) || alert.getText().equals(alertmessage2))
      {
       System.out.println(alert.getText());
       Thread.sleep(3000);
     // driver.findElement(By.xpath("//*[@id=\"menuitem_form\"]/div[11]/button[1]")).click();
       alert.accept();//Click on OK button
      break;
      }
     else
      {
       System.out.println("Menuitem Not created");

     }
      Thread.sleep(3000);
     // driver.findElement(By.xpath("//*[@id=\"menuitem_form\"]/div[11]/button[1]")).click();
       alert.accept();//Click on OK button

    }
        }
    }
    }

儿童班

package sanityTest;
import sanityTest.AdminLogin;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class EditMenuItem extends AdminLogin {



  @Test //(priority = 1)
  public void editMenu() throws InterruptedException {

      Thread.sleep(2000);

      //Click On MENU
      driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[4]/a")).click();
      System.out.print("click on MENU");

      //Select Display Menu Item
      driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[4]/ul/li[4]/a")).click();
      System.out.println("Menu Item List opened");

      //driver.close();
  }

xml类

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Menu">
  <test name="sanityTest">
    <classes>
      <class name="sanityTest.AdminLogin"/>
     <class name="sanityTest.EditMenuItem"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

【问题讨论】:

  • 你展示了一些永远不会遵守的代码,没有任何关于它应该测试什么的上下文。我们到底在这里尝试什么?你能展示你的实际代码吗?另外:如果 ChildClass 实际上扩展了 ParentClass,那么它也继承了其中的方法(测试)。
  • 您好,欢迎来到 Stack Overflow。在您的示例中 ChildClass 没有扩展 ParentClass 但我想是这样。当一个类扩展另一个类时,它继承了它的所有方法。在这种特定情况下,它将继承所有 @Test 注释方法。执行套件后,两个类都将被执行,实际上您的子类包含 3 个测试,其中两个与父类中的测试完全相同。
  • @Stultuske 我已经用实时代码更新了我的帖子
  • @SergioArrighi 有什么解决办法吗?
  • EditMenuItem 扩展了 AdminLogi,这意味着当您在 EditMenuItem 中执行测试时,您还将从 AdminLogin 执行这些测试。这是基本的继承。

标签: java xml selenium-webdriver testng testcase


【解决方案1】:

根据 JAVA 继承概念,如果我们将任何类扩展到其子级,则将执行第一个父级,然后执行子级。仅仅因为这个概念,我们就能够在Java中实现方法覆盖(用子方法覆盖父方法)。 谈到您的情况,您通过扩展 AdminLogin 类将“AdminLogin”编写为父类,并将“EditMenuItem”编写为子类。因此,每当您尝试执行子类(EditMenuItem)时,将执行第一个父类(AdminLogin),然后执行子类。

接下来是 pom.xml,在这里你首先调用父类,然后是子类。 按照给定的顺序,将首先执行父类,然后执行子类。当 selenium 尝试执行 child 时,它也应该执行 parent(根据继承概念)。这就是在您的情况下父类运行两次的原因。

要解决此问题,请不要将 AdminLogin 类扩展到您的子类并保持 pom.xml 原样。通过这种方式,First AdminLogin 将被执行,然后您提到的子类作为 pom.xml 文件中层次结构中的第二个类。

【讨论】:

    【解决方案2】:

    说实话,我没有深入研究您的测试,我看不出您需要继承的真正原因。但是您可以尝试将父类标记为抽象。这样测试只会在子类中执行。

    我在 c# 和 Nunit 中使用过这种技术,但我猜想它在 Java 中的工作方式类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 2014-05-31
      • 1970-01-01
      相关资源
      最近更新 更多