使用TestNG注释时,方法没有运行

时间:2016-05-11 15:04:33

标签: selenium

//在下面运行时,@ Test和@After方法代码没有运行。使用Selenium-jave-2.54.jar和TestNG

//创建管理员

import java.util.Scanner;

public class lab13 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        double[] priceArray = {0, 0, 0, 0};

        for(int counter = 0, counter < priceArray.length, counter++)        
            System.out.println("Please enter a price");
            Double priceArray[counter] = Double.parseDouble(input.nextLine());


    }

}

//打开浏览器

public class createAdmin1 {

    public String url = "http://10.120.6.12:8080/#/login";
    public WebDriver driver;
    //@SuppressWarnings("unused")

//登录服务器并创建管理员

@BeforeMethod
      public void Login() {

          WebDriver driver = new FirefoxDriver();
         // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.navigate().to(url);


      }

/ * //使用TestNG运行它时出错

@Test
  public void CreateAdmin() {

        /*driver.get(url);
        driver.findElement(By.id("username")).sendKeys("at.pretendcompany@gmail.com");
        driver.findElement(By.id("password")).sendKeys("abc123");

        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div/div/div[2]/button")).click();

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        */
        //driver.get(url);
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //driver.navigate().to(url);
        driver.findElement(By.id("username")).sendKeys("test");
        driver.findElement(By.id("password")).sendKeys("test123");

        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div/div/div[2]/button")).click();

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.findElement(By.id("snAdmins")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div/div[1]/div/div/div[2]/button[3]")).click();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.findElement(By.id("adminId")).sendKeys("user1@gmail.com");
        driver.findElement(By.id("password")).sendKeys("user1");
        driver.findElement(By.id("pwConfirmInput")).sendKeys("user1");

        driver.findElement(By.id("adminQuestion")).sendKeys("Who are you?");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(By.id("adminAnswer")).sendKeys("I am admin");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        System.out.println(driver.findElement(By.xpath("//button[contains(.,'Save')]")).isDisplayed());
        driver.findElement(By.xpath("//button[contains(.,'Save')]")).click();

  }
  // Close the browser after create admin finished.

  @AfterMethod
  public void Quit() {


    driver.close();
        System.out.println("Driver closed");
  }

}

2 个答案:

答案 0 :(得分:0)

最高用途

      public static WebDriver driver;

而不是公共WebDriver驱动程序;

在方法使用之前如下

  @BeforeMethod
  public void Login() {

      driver = new FirefoxDriver();

谢谢你, 穆拉利

答案 1 :(得分:0)

这是因为您已在代码顶部声明了“公共WebDriver驱动程序”。现在你要做的就是不再在@BeforeMethod中声明它。现在这应该有效。

@BeforeMethod

public void Login(){

     //See here we are just calling the already declared WebDriver.
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.navigate().to(url);
      driver.manage().window.maximize();



  }