简体   繁体   中英

how to Pass command line argument ( baseURL) using java for selenium WebDriver

i have created a runnable jar for my Selenium webDriver suite. now i have to test this in multiple base URL,As the baseURl will change according to the need. Can somebody please guide me through this. Is it possible to send command line arguments to Selenium Web Driver driver.get(baseUrl)

This code i have written but doesn't work.

public class news {

    public static void main(String[] args) {
        FirefoxDriver driver = new FirefoxDriver();

        try{
              JavascriptExecutor js = (JavascriptExecutor)driver;
              js.executeScript("var pr=prompt('Enter your URL please:',''); alert(pr);");  
              Thread.sleep(15000L);
              String URL = driver.switchTo().alert().getText();
              driver.switchTo().alert().accept();
              System.out.println(URL);
              driver.get(URL);
              System.out.println(URL);
            }catch(Throwable e)
              {
              System.out.println("failed");
            }
        driver.manage().window().maximize();

        WebElement login=driver.findElement(By.cssSelector("a[data-target='#login-box']"));
        login.click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        driver.findElement(By.id("uname_h")).sendKeys("Abc@gmail.com");
        driver.findElement(By.id("password_h")).sendKeys("XYZ");
        WebElement login_button=driver.findElement(By.id("health_btn"));
        login_button.click();
       // driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        driver.findElement(By.className("glyphicon-publish-news-events")).click();
        driver.findElement(By.xpath("/html/body/div[4]/div[4]/div[26]/div/ul/li[2]/a")).click();
        driver.findElement(By.id("newstitle")).sendKeys("Automation testing news");
        driver.findElement(By.id("newsdetails")).sendKeys("Automation testing news datails");
        WebElement webElement13 = driver.findElement(By.id("s2id_autogen3"));
        webElement13.sendKeys("ganesh");
        webElement13.sendKeys(Keys.ENTER);
}
}

You can add below code:-

Scanner scanner = new Scanner(System.in);

    //  prompt for the URL
    System.out.print("Enter your URL: ");

    // get their input as a String
    String URL = scanner.next();

Now url will be asked in terminal

Now pass this URL variable in you below line code:-

  driver.get(URL);

My below working code:-

  WebDriver driver=null;

Scanner scanner = new Scanner(System.in);

    //  prompt for the URL
    System.out.print("Enter your URL: ");

    // get their input as a String
    String URL = scanner.next();



    System.out.println( URL );
    final FirefoxProfile firefoxProfile = new FirefoxProfile();
    driver = new FirefoxDriver(firefoxProfile);
    driver.manage().window().maximize();

    driver.get(URL);

Hope it will help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM