为什么eclipse不喜欢" driver = new FirefoxDriver(ffBinary,firefoxProfile);"

时间:2017-11-06 01:53:41

标签: java eclipse google-chrome selenium

我正在尝试跟进时间敏感的硒课程,而我正在失去绝望。我只能在第一次使用chrome而不是firefox执行此测试,但后来我开始收到连接到chrome的错误并返回到firefox。以下是教程课程中应该运行的脚本。我已经在我的系统变量中添加了firefox.exe的路径,并且我正在运行firefox 47,我将其降级以查看是否有帮助。我使用的是JDK 1.8和selenium 3.6.0。这是课程的代码。我的日食是在线路驱动器=新的FirefoxDriver(ffBinary,firefoxProfile);红色下划线要求"删除此参数以匹配firefoxDriver"。

如果我按照说明删除它,因为只有driver = new Firefox Driver,我就会收到此错误:

线程中的异常" main" java.lang.Error:未解决的编译问题: 在Guru99package.TestScript02.main(TestScript02.java:72)

第72行是整个主要功能的起始线,所以这对我来说真的不清楚。据我所知,它不能编译的原因可能是任何东西,firefox路径的语法与我在github教程和google中看到的一致。

我尝试在chrome中执行此操作并且可以在此处粘贴我的结果,但我在chrome中也非常困惑我是否应该使用webdriver或RemoteWebDriver来实例化我的webdriver。当我这样打电话给Chrome时:

 System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
 WebDriver driver = new ChromeDriver();

我的脚本最初运行但不会在登录字段中输入任何内容。现在似乎我所做的一切都在起作用。该教程没有论坛或帮助选项。

主文件的代码:

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;




public class TestScript02 {

static WebDriver driver; // Selenium control driver
private static String baseUrl; // baseUrl of Website Guru99

// This method SetUp will read initialization parameters from the          classUtil.java & launch Firefox 

public static void setUp() throws Exception {
/*
 * Tells the Selenium client library to connect to the Webdriver
 * service using firefox
 * 
 * In some PC's, Selenium can not find the binary file of Firefox because
 * user doesn't install Firefox at its default location. We need to tell
 * Selenium where the firefox.exe is
 */
    File pathToBinary = new File(Util.FIREFOX_PATH);
    FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);

/*
 * Create new firefoxProfile for Testing
 * 
 * A profile in Firefox is a collection of bookmarks, browser settings,
 * extensions, passwords, and history; in short, all of your personal
 * settings. Firefox uses a DEFAULT profile to store all of your
 * personal settings.
 * 
 * In this case, we use Firefox for "testing" purpose not as an "end user".
 * We need to create NEW firefoxProfile because we want to separate the
 * Firefox profile for testing purpose and that of an end user. If
 * something wrong happens with the testing, you still have your DEFAULT
 * profile to fall back to (your personal data still safe).
 */
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    driver = new FirefoxDriver(ffBinary, firefoxProfile);

// Setting Base URL of website Guru99
baseUrl = Util.BASE_URL;
// Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
// Refer - http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html
driver.manage().timeouts()
    .implicitlyWait(Util.WAIT_TIME, TimeUnit.SECONDS);
// Go to http://www.demo.guru99.com/V4/
driver.get(baseUrl + "/V4/");
}

/**
 * 
 * @author Krishna Rungta
 *        Test Script 02
 *        ************** 
 *        This method will perform following Test Steps
 *        
 *        1)  Go to http://www.demo.guru99.com/V4/
          2) Enter valid UserId
          3) Enter valid Password
          4) Click Login
          5) Verify the Page Title after login
 */

public static void main(String[] args) throws Exception {



String username, password;
String actualTitle;
String actualBoxtitle;


    //Setup Firefox driver
    setUp();


    driver.findElement(By.name("uid")).clear(); // Good practice to clear a field before use
    driver.findElement(By.name("uid")).sendKeys(Util.USER_NAME);  // Enter username


    driver.findElement(By.name("password")).clear(); // Good practice to clear a field before use
    driver.findElement(By.name("password")).sendKeys(Util.PASSWD);  // Enter Password

    // Click Login
    driver.findElement(By.name("btnLogin")).click();

    actualTitle = driver.getTitle();
    if (actualTitle.contains(Util.EXPECT_TITLE)) {
                System.out.println("Test case: Passed");
    } 
    else {
                System.out.println("Test case : Failed");
    }

    driver.close();



}

}

util.java的代码:

package Guru99package;

public class Util {

/* You can change the Path of FireFox based on your environment here */
public static final String FIREFOX_PATH = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";

// Setting Base URL
public static final String BASE_URL = "http://www.demo.guru99.com/";

// Time to wait when searching for a GUI element 
public static final int WAIT_TIME = 30; 


// Valid account for login
public static final String USER_NAME = "mngr103067";
public static final String PASSWD = "pepysYd";


// Expected output
public static final String EXPECT_TITLE = "Guru99 Bank Manager HomePage";


}

0 个答案:

没有答案