简体   繁体   中英

How to get user input using selenium webdriver java

I have to build a java project which has to complete the following use cases:

  1. get the url from user: for this need to show a alert window to the user to get the url.If user clicked on Cancel , close the programe.

  2. Open the entered url in browser: Once the user has entered the url and clicked on Ok, the browser should show that url.

  3. Let the user close the browser window

  4. Once the user closes the browser window go back to prompt window to ask the user do you want to continue or quit. If contiue then again open the window to enter url else close.

How can I achieve all the above steps in java and selenium? Please help. EDIT: Adding the code that I have so far.

    Scanner reader=new Scanner(System.in);
    System.out.println("Enter the endpoint to hit");
    String url=reader.next();  

    String classLoader = getClass().getClassLoader().getResource("file.txt").getFile();
    Scanner in =new Scanner(new File(classLoader));

    while(in.hasNext()){
        String line=in.nextLine();
        String formedUrl=url+line;
        BaseSeleniumSetup.setupChromeDriver(formedUrl);
    }

    in.close();

 public class BaseSeleniumSetup {
    public static void setupChromeDriver(String url) throws InterruptedException {
        WebDriver driver= new ChromeDriver();
        driver.get(url);
        Thread.sleep(2000);
        driver.quit();
    }
}

You can use library import javax.swing.JOptionPane; and the following steps

String url = JOptionPane.showInputDialog(null,"Enter URL"); //To create window
WebDriver driver =new ChromeDriver(); //To open browser
driver.get(url); // To open url in browser

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