Selenium 只有@BeforeMethod被执行,而@Test和@After方法失败
这是我的密码:Selenium 只有@BeforeMethod被执行,而@Test和@After方法失败,selenium,testng,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; im
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"));
}
}
我正在尝试在
中执行上述代码,只有@beforethod
正在执行,并且
和>方法失败后,
任何人可以在这方面帮助我吗???在before方法中从这一行删除webdriver,只需使用驱动程序
WebDriver driver=new FirefoxDriver();
仅使用
driver=new FirefoxDriver();
作出以下更改:-
WebDriver driver=null;
@BeforeMethod
public void beforeMethod() throws Exception {
driver=new FirefoxDriver();
driver.get("http://lwscomsit4.lowes.com/");
Thread.sleep(10000);
}
我试图执行你的代码,收到拒绝访问错误?可能是网站不向所有人开放?如果您可以发布stacktrace或提供失败的详细信息,这也会很有帮助。请在Before方法中插入一些断言,以确保它已准备好进行测试。请将url更改为www.lowes.com您正在做的一个错误是没有使用
WebDriver=new FirefoxDriver()代码>使用驱动程序=新的FirefoxDriver()
在BeforeMethod
中,因为您希望使用驱动程序实例变量,并且它应该在所有方法中都可见。你得到的另一个错误是什么,你能粘贴你的堆栈跟踪吗?