Selenium Webdriver中的编译错误:字段package.Classfile.driver不可见

时间:2017-07-05 22:55:07

标签: selenium-webdriver

我似乎遇到了某种编译错误。

我在'base'包中有'Common.java'类。这是一个启动firefox驱动程序的程序。那,我把它保存在单独的包中,使其成为一次性的努力和模块化目的。

我在子类'tc_01.java'中调用此类文件。这个TC_01.jave程序在另一个包'测试'中。这个TC_01.java文件实际上是从Common.java访问驱动程序并启动浏览器并尝试一些登录和注销操作。

我的子类TC_01.java向我显示编译错误,鼠标悬停上的错误消息是 - > “字段Common.driver不可见”。 并且,在控制台:“ java.lang.Error:未解决的编译问题:     字段Common.driver不可见

我的分析:似乎TC_01.java文件无法从'Common.java'访问'driver'。 但是,我已经编写了'extends'关键字来继承属性。

请指导我。感谢

这是我的代码: - >

package base;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class Common {

public FirefoxDriver driver;

@BeforeMethod
public void BM(){
System.setProperty(“webdriver.gecko.driver”,”D://Rajiv//Selenium//geckodriver.exe”);
driver = new FirefoxDriver();
driver.get(“http://automationpractice.com/index.php”);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@AfterMethod
public void CM(){
driver.close();
}
}

# Pakage – testing; Class – Tc_01.java

package testing;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import base.Common;

public class TC_01 extends Common{
public FirefoxDriver driver;`
@Test
public void TM(){
System.out.println(“Selenium Webdriver Script in Firefox browser using Gecko` `Driver | AutomationPractice.com PAGE LAUNCHED”);

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“search_query_top”)));

try{
    String expectedTitle = “My Store”;
    System.out.println(“ExpectedTile = “+expectedTitle );
    String actualTitle = driver.getTitle();
    System.out.println(“The actual Title of the Page is = “+actualTitle);
   Assert.assertEquals(actualTitle, expectedTitle);*/

   System.out.println(“Control has reached here”);
   driver.findElementByClassName(“login”).click(); // field common.driver is not visible
   driver.findElementById(“email”).sendKeys(“*****@yahoo.com”);
   driver.findElementById(“passwd”).sendKeys(“*****”);
   driver.findElementById(“SubmitLogin”).click();

   driver.findElementByClassName(“logout”).click();
   System.out.println(“Sucessfully Logout from the Application”);
   }catch (Exception e){
      e.printStackTrace();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的根本原因。 在我在这个论坛上寻求帮助之前我浪费了3天。 正如我最初分析的那样,我在父类中提到过我的驱动程序属性,并在Child Class中访问它。有一些名称不匹配。当它试图从父级继承属性时,它无法访问并给我编译错误。我将父类名重命名为我在Child Class中扩展时给出的名称。而且,它奏效了。 再次感谢大家。这是一个非常好的论坛,可以在彼此之间讨论您的问题并获得解决方案。