Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要一些帮助来理解这个Selenium测试脚本示例吗_Java_Selenium - Fatal编程技术网

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

Java 需要一些帮助来理解这个Selenium测试脚本示例吗,java,selenium,Java,Selenium,我回顾了这个例子,试图理解它是如何工作的。这一例子取自 步骤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)

我回顾了这个例子,试图理解它是如何工作的。这一例子取自

步骤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应以静态方式访问”

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


谢谢。

可以直接在类上调用静态方法,而无需实例化类的对象

PageObjectsPercCalc.lnk_math_calc(driver).click();

非常感谢您的回复。这解决了我的问题。不确定教程点站点为什么会放置“'page_objects_perc_calc.lnk_math_calc(驱动程序)。单击();”而不是“PageObjectsPerc.lnk_math_calc(驱动程序)。单击();”