只有@BeforeMethod被执行并且@Test和@After方法失败了

时间:2015-12-23 06:09:15

标签: selenium testng

这是我的代码:

package lowesautomation;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class StorePickUP {

public WebDriver driver;

    @BeforeMethod
      public void beforeMethod() throws Exception {
          WebDriver driver=new FirefoxDriver(); 

             driver.get("http://lwscomsit4.lowes.com/");
             Thread.sleep(10000);
      }

    @Test
      public void Test() throws Exception {
          Thread.sleep(5000);
          driver.findElement(By.id("nav-search-input")).click();
          driver.findElement(By.id("nav-search-input")).sendKeys("29708");
          driver.findElement(By.xpath("html/body/div[3]/div[3]/div[1]/ul/li[4]/form/fieldset/button")).click(); 
          Thread.sleep(8000);
          driver.findElement(By.xpath("html/body/div[3]/div[3]/div[3]/ul/li[1]/form/span/input")).sendKeys("138430");
          driver.findElement(By.xpath("html/body/div[3]/div[3]/div[3]/ul/li[1]/form/span/button")).click();
        //add to cart
          driver.findElement(By.xpath("html/body/div[2]/div/div[4]/div/div/div[3]/div[2]/div[1]/form/ul[3]/li[2]/div/a/span")).click();
          driver.navigate().refresh();
        Thread.sleep(3000);
        driver.findElement(By.name("shipModeId_1")).click();
        driver.navigate().refresh();
        driver.findElement(By.xpath(".//*[@id='ShopCartForm']/div[2]/div[2]/a[2]/span")).click();
        driver.findElement(By.xpath(".//*[@id='login-container']/div[2]/div/div/div/a/span")).click();
        Thread.sleep(3000);
        driver.findElement(By.xpath(".//*[@id='checkout-card-type']")).sendKeys("Lowe's consumer credit card");
        driver.findElement(By.name("cardNumber")).sendKeys("81100066123928");
        driver.findElement(By.name("sCode")).sendKeys("686");
        //driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div/form/div[2]/fieldset/ol/li[4]/div/select[1]")).sendKeys("12-DEC");
      //driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div/form/div[2]/fieldset/ol/li[4]/div/select[1]")).sendKeys("2015");
      driver.findElement(By.xpath(".//*[@id='billingFname']")).sendKeys("chaitra");
      driver.findElement(By.xpath(".//*[@id='billingLname']")).sendKeys("aswathanarayana");
      driver.findElement(By.xpath(".//*[@id='billingAddress1']")).sendKeys("Test Store Philadelphia");
      driver.findElement(By.xpath(".//*[@id='billingCity']")).sendKeys("philadelphia");
      driver.findElement(By.xpath(".//*[@id='billingState']")).sendKeys("Pennsylvania");
      driver.findElement(By.xpath(".//*[@id='billingZip']")).sendKeys("19114");
      driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div/form/div[3]/fieldset/ol/li[2]/div/input")).sendKeys("8105575328");
      driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div/form/div[3]/fieldset/ol/li[3]/div/input")).sendKeys("r6sitemail4@lowes.qa");
      driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div/div[2]/div/div/div[2]/div/form/div[5]/ul/li[3]/a/span")).click();
  }


    @AfterMethod
        public void afterMethod() throws Exception {
            String confno=driver.findElement(By.xpath(".//*[@id='conf-main']/div[2]/div[2]/div[1]/div/div[1]/div[1]/strong[2]")).getText();
                System.out.println("confnumber:"+confno);
                Thread.sleep(5000);
                    TakesScreenshot ts=(TakesScreenshot)driver;
                    File Source=ts.getScreenshotAs(OutputType.FILE);
                    FileUtils.copyFile(Source, new File("./screensh/screenshot4.png"));
  }

}

我正在尝试在 TestNG 中执行上述代码,只有 @BeforeMethod 正在执行且 @Test < / strong>和 @After 方法失败了,任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

在方法之前从此行中删除webdriver,只需使用驱动程序

WebDriver driver=new FirefoxDriver();

仅使用

 driver=new FirefoxDriver();

答案 1 :(得分:0)

进行如下更改: -

WebDriver driver=null;

@BeforeMethod
      public void beforeMethod() throws Exception {
         driver=new FirefoxDriver(); 

         driver.get("http://lwscomsit4.lowes.com/");
         Thread.sleep(10000);
 }