我是一名新手,想通過測試參數化用戶名和密碼字段。這個腳本只爲我打開URL,不做任何事情。有人能幫助我,並讓我知道實現這一目標的適當方式嗎?無法在Selenium中使用TestNG進行參數化設置
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Testcase1 {
WebDriver driver=new FirefoxDriver();
driver.get("URL");
// driver.manage().timeouts().pageLoadTimeout(60,TimeUnit.SECONDS);
@Test
@Parameters({"sUsername","sPassword"})
public void login()
{
WebElement objUsername = driver.findElement(By.xpath("//input[@placeholder='Username']"));
objUsername.sendKeys("sUsername");
WebElement objPassword = driver.findElement(By.xpath("//input[contains(@placeholder,'Password')]"));
objPassword.sendKeys("sPassword");
WebElement objLogin = driver.findElement(By.xpath("//span[contains(text(),'Login')]"));
objLogin.click();
if (driver.findElement(By.xpath("//a[.='LOGOUT']")).isDisplayed())
{
System.out.print("Test Case is Passed");
}
else
System.out.print("Test Case is Failed");
}
}
XML:
import java.util.concurrent.TimeUnit;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke">
<test name="Testcase1">
<parameter name="sUsername" value="testuser"></parameter>
<parameter name="sPassword" value="123"></parameter>
<classes>
<class name="org.param.testcases.Testcase1">
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
感謝您的幫助,現在該腳本需要用戶名和傳遞來自xml的單詞。但是,它不會導航到下一個屏幕,並會失敗並顯示異常。 – sgaurav
你會得到什麼錯誤? – kushal
現在它的工作原理是我在腳本中傳遞字符串而不是變量 – sgaurav