package webdriver3;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestingAmazon {
WebDriver driver;
@BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.get("http://google.co.in");
driver.manage().window().maximize();
openAmazon();
}
public void openAmazon(){
driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("Amazon india");
driver.findElement(By.xpath("//div[@id='sblsbb']/button")).click();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[text()='Amazon']")).click();
hoverLogin();
}
public void hoverLogin(){
Actions builder=new Actions(driver);
WebElement mainmenu= driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']/span[1]"));
builder.moveToElement(mainmenu).build().perform();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='nav-flyout-ya-signin']/a/span")));
WebElement submenu=driver.findElement(By.xpath("//div[@id='nav-flyout-ya-signin']/a/span"));
submenu.click();
Reporter.log("Done.....",true);
}
@Parameters({"userName","password"})
@Test
public void enterCreditial(String userName, String password){
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement loginbox= driver.findElement(By.xpath("//input[@id='ap_email']"));
WebElement passwordbox= driver.findElement(By.xpath("//input[@id='ap_password']"));
WebElement signin= driver.findElement(By.xpath("//input[@id='signInSubmit']"));
if(loginbox.isDisplayed()){
Reporter.log("You can enter log in name");
loginbox.sendKeys(userName);
if(passwordbox.isDisplayed()){
Reporter.log("You can enter password");
passwordbox.sendKeys(password);
if(signin.isDisplayed()){
Reporter.log("You can signin");
signin.click();
}
}
}
}
public void informationCommand(){
String titletext=driver.getTitle();
System.out.println("Title is:" + titletext);
String pageurl= driver.getCurrentUrl();
System.out.println("Page url:" + pageurl);
String windowhandle= driver.getWindowHandle();
System.out.println("Windowhandle:" + windowhandle);
Set<String>windowhandles= driver.getWindowHandles();
System.out.println("Windowhandles:" + windowhandles);
}
public void signout(){
Actions builder=new Actions(driver);
WebElement mainmenu= driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']/span[1]"));
builder.moveToElement(mainmenu).build().perform();
WebDriverWait wait=new WebDriverWait(driver,5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='nav-item-signout']/span")));
WebElement signout=driver.findElement(By.xpath("//a[@id='nav-item-signout']/span"));
signout.click();
Reporter.log("Successfuly signout.......",true);
driver.quit();
}
}
当我使用TestNG脚本运行此程序时,出现以下错误:
@Test在方法enterCreditial上需要参数'userName',但尚未标记为@Optional或已定义 在C:\ Users \ jahansalia \ AppData \ Local \ Temp \ testng-eclipse-1960767273 \ testng-customsuite.xml
答案 0 :(得分:1)
我看起来你没有使用testng.xml
用于填充此方法参数的变量列表。必须在testng.xml文件中定义这些变量。例如
@Parameters({ "xmlPath" })
@Test
public void verifyXmlFile(String path) { ... }
并且在testng.xml中你需要提供这样的细节:
<parameter name="xmlPath" value="account.xml" />
参考: -
http://www.tutorialspoint.com/testng/testng_parameterized_test.htm