0
我在回顧這個例子並試圖理解它是如何工作的。該示例取自http://www.tutorialspoint.com/selenium/selenium_page_object_model.htm。需要一些幫助來理解這個Selenium測試腳本示例
第一步:
package PageObject;
import org.openqa.selenium.*;
public class PageObjectsPercCalc
{
private static WebElement element = null;
// Math Calc Link
public static webElement lnk_math_calc(WebDriver driver)
{
element = driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a"));
return element;
}
//Percentage Calc Link
public static webElement lnk_percent_calc(WebDriver driver)
{
element = driver.findElement(By.xpath(".//*@id='menu']/div[4]/div[3]/a"));
return element;
}
//Number 1 Text Box
public static webElement txt_num_1(WebDriver driver)
{
element = driver.findElement(By.id("cpar1"));
return element;
}
//Number 2 Text Box
public static webElement txt_num_2(WebDriver driver)
{
element = driver.findElement(By.id("cpar2"));
return element;
}
//Calculate Button
public static webElement btn_calc(WebDriver driver)
{
element = driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input"));
return element;
}
// Result
public static webElement web_result(WebDriver driver)
{
element = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b"));
return element;
}
}
第2步:使用對象庫上面創建以下測試:
package PageObject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PercentCalculator {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.calculator.net");
//Use page Object library now
page_objects_perc_calc.lnk_math_calc(driver).click();
page_objects_perc_calc.lnk_percent_calc(driver).click();
page_objects_perc_calc.txt_num_1(driver).clear();
page_objects_perc_calc.txt_num_1(driver).sendKeys("10");
page_objects_perc_calc.txt_num_2(driver).clear();
page_objects_perc_calc.txt_num_2(driver).sendKeys("50");
page_objects_perc_calc.btn_calc(driver).click();
String result = page_objects_perc_calc.web_result(driver).getText();
if(result.equals("5")){
System.out.println(" The Result is Pass");
}
else {
System.out.println(" The Result is Fail");
}
driver.close();
}
}
現在我的問題是如何「page_objects_perc_calc」對象調用庫方法沒有宣佈它作爲第二步中的PageObjectsPercCalc?現在,如果我把它聲明如下:
PageObjectsPercCalc page_objects_perc_calc = new PageObjectsPercCalc();
我得到這樣的警告
「從類型 PageObjectsPercCalc應以靜態的方式來訪問靜態方法lnk_math_calc(webdriver的)」;
這是正確的,但我不知道如何定義PageObjectsPercCalc
對象爲靜態。請幫忙。
謝謝。
非常感謝您的回覆。這解決了我的問題。不知道爲什麼tutorialpoint網站放置「'page_objects_perc_calc.lnk_math_calc(driver).click();」而不是「PageObjectsPercCalc.lnk_math_calc(driver).click();」 – mojo1012