需要一些帮助来理解这个Selenium测试脚本示例

时间:2016-03-04 03:32:55

标签: java selenium

我正在审核此示例,并尝试了解其工作原理。该示例来自http://www.tutorialspoint.com/selenium/selenium_page_object_model.htm

步骤1:

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();

我收到此警告

  

"静态方法lnk_math_calc(WebDriver)来自该类型   应以静态方式访问PageObjectsPercCalc&#34 ;;

这是正确的,但我不知道如何将PageObjectsPercCalc对象定义为静态。请帮忙。

感谢。

1 个答案:

答案 0 :(得分:0)

可以直接在类上调用静态方法,而无需实例化类的对象。 PageObjectsPercCalc.lnk_math_calc(驱动程序)。单击();