2

我应该在哪里添加以下语句 System.setProperty("webdriver.gecko.driver","//home//tuser//software//selenium//gecko_driver//geckodriver");

这样我的代码就不会产生错误

public class class1 {

public static String baseURL = "https://facebook.com/";
public WebDriver driver = new FirefoxDriver();

@Test(priority = 1)
public void login() throws InterruptedException {

    driver.get(baseURL);
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys("srikanth");
    driver.findElement(By.id("password")).sendKeys("sri");
    driver.findElement(By.id("login_button")).click();
    Thread.sleep(30000);
}
4

3 回答 3

1

我使用了 Jainish 提供的答案,但对我没有用。同样,我发现了以下解决方案:

    public class class1 {

    //public WebDriver object so that it can be used anywhere within class1
    public WebDriver driver; 

    //public WebDriverWait object so that it can be used anywhere within class1
    public WebDriverWait wait;

    @BeforeClass
    public void setup()
        {
            System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
            driver = new FirefoxDriver();
            wait = new WebDriverWait(driver,15); //explicit wait to increase performance
        }

    @Test()
    public void login() {

        driver.get("https://facebook.com/");
        driver.manage().window().maximize();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
        driver.findElement(By.id("username")).sendKeys("srikanth");
        driver.findElement(By.id("password")).sendKeys("sri");
        driver.findElement(By.id("login_button")).click();

        }
    }

我修改了 Jainish 编写的代码。在修改后的代码中,您可以看到对象是在任何方法之外以公共范围声明的,因此,我们可以在class1中的任何位置使用该对象。

此外,无需同时使用静态等待(即 Thread.sleep)和隐式等待。而是使用显式等待来提高脚本性能。

希望以上内容有用。如果我在任何地方错了,请告诉我。

于 2018-01-05T19:24:31.433 回答
0

请参阅下面的片段

public class class1 {
@Test(priority = 1)
public void login() throws InterruptedException {
    String baseURL = "https://facebook.com/";
    System.setProperty("webdriver.gecko.driver","C:\\Selenium Training\\Practice\\Firefox driver\\geckodriver.exe"); //Make sure to provide .exe extn
    WebDriver driver = new FirefoxDriver();
    driver.get(baseURL);
    driver.manage().window().maximize();
    driver.findElement(By.id("email")).sendKeys("srikanth");
    driver.findElement(By.id("pass")).sendKeys("sri");
    driver.findElement(By.id("u_0_n")).click();
    Thread.sleep(30000);
    }
  }
于 2016-12-21T07:19:09.783 回答
0
public class class1 {

@Test
public void setup() throws InterruptedException
    {
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);        
    }

@Test(dependsOnMethods = {"setup"})
public void login() throws InterruptedException {

    driver.get("https://facebook.com/");
    Thread.sleep(2000);
    driver.manage().window().maximize();
    driver.findElement(By.id("username")).sendKeys("srikanth");
    driver.findElement(By.id("password")).sendKeys("sri");
    driver.findElement(By.id("login_button")).click();
    Thread.sleep(5000);
    }
}
于 2017-01-06T07:15:44.903 回答