在TestNG测试用例中保留system.property的位置

时间:2016-12-20 19:33:09

标签: java selenium automated-tests testng geckodriver

我应该在哪里添加以下声明     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);
}

3 个答案:

答案 0 :(得分: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);
    }
  }

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

我使用了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)并同时隐式等待。而是使用显式等待来提高脚本性能。

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