3

I'm currently taking my first automated test class and the instructor has us creating a program in Eclipse after loading Selenium and create a step in the program to look at an executable to bring up chrome then designate a website to check. It looks like i am stuck in a loop?

Here is the program: java program

Here is the result: program result

any and all help would be appreciated. Thank you for your time.

WillieV
  • 31
  • 1

1 Answers1

1

I think this is what you want

This code is to open the default browser and go to a specific link You can specify the path of any browser you want from the path in the code

import java.awt.Desktop;
import java.net.URI;

public class openBrowser {

    public openBrowser() {
        try {
            if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                Desktop.getDesktop().browse(new URI("https://www.google.com"));
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
        public static void main(String[]args) {
            new openBrowser();
        }
}

For your code you can follow the following steps

  • Download ChromeDriver from here
  • Extract the zip file and follow the path ( because it is easy ) C:\\chromeDriver\\chromedriver.exe
  • include the ChromeDriver location in your PATH environment variable
  • Download the required Libraries from the following junit openqa
  • Add the Libraries to your project ( Build Path )
  • then this is your code

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.*;
    import org.junit.Test;
    
    public class WebDriverDemo {
        @Test
        public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromeDriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("ChromeDriver");
        searchBox.submit();
            try {
                Thread.sleep(10000);
    
            } catch (InterruptedException ex) {
                Logger.getLogger(WebDriverDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
            driver.quit();
        }
    }
    

During the implementation of the code in the eclipse, many problems occurred, so I advise you to implement the project on NetBeans I use Java 8 and Windows 8.1

Wassim Al Ahmad
  • 1,102
  • 2
  • 7
  • 23
  • Thank you for your input Wassim. Your suggestion worked perfectly. Just out of curiosity, If i wanted to try to use the code that was suggested from the instructor , how or what changes would have to be made to that code if possible? Please let me know at your earliest convenience. Thank you again. – WillieV Dec 16 '19 at 22:48
  • Very sorry for the late reply – Wassim Al Ahmad Dec 17 '19 at 23:01