How to Configure Selenium in IntelliJ and an Overview of Launching Browsers / Blogs / Perficient


In this blog, our focus will be on creating a Maven project in IntelliJ and launching multiple browsers using the Selenium WebDriver.

What is Selenium and WebDriver?

Selenium is a web UI automation testing suite that is used to automate web applications for testing purposes. Selenium supports automation across different browsers (Google Chrome, Microsoft Edge, Mozilla Firefox), platforms (Windows, Linux, Solaris), and programming languages (C#, Java, Perl, PHP, Python). Basically, it is a framework where scripts are written to run a WebDriver, which in turn controls the browser.

WebDriver is an API that drives the web browser by using libraries and commands.

  • To configure Selenium in IntelliJ, the user should have JDK installed on the OS.

Steps to Download and Install JDK

  1. Download the JDK based on system specifications: https://www.oracle.com/java/technologies/downloads/#jdk18-windows
  2. Open the downloaded file and click on Next.Picture1
  3. Give the path where you want JDK to be installed.Picture2
  4. Once it gets installed on your device, a dialogue box will appear as shown below.Picture3
  5. The next step is to configure environment variables and to set the environment variables, search for Environment Variables in the Task Bar and open the same.
  6. Click on Environment Variables as shown below.Picture4
  7. Select the Path and click on Edit.Picture5
  8. Click on New and enter the Path address i.e. C:\Program Files\Java\jdk-20\bin.
  9. Click on OK.Picture6
  10. Under System Variables, click on New.Picture7
  11. Give the Variable name JAVA_HOME and the Variable value C:\Program Files\Java\jdk-20 (The variable value is the path where JDK is installed).
  12. Click on OK.Picture8
  13. To check whether the process has been completed successfully or not, open the Command Prompt, enter the command java -version, and check the status as shown below.Picture9

Steps to Download and Install IntelliJ

  1. To download IntelliJ, go to the site: https://www.jetbrains.com/idea/download/ and select the .exe file of the community version. The file should start downloading.
  2. Once the download is complete, open the.exe file and you should see a pop-up window as shown below. Click on Next.Picture10
  3. Select the destination folder where you want IntelliJ to get installed, and then again click on Next.Picture1
  4. Select the checkbox as per the requirements, and then click on Next.Picture12
  5. Choose the start menu folder, or by default, it is set to JetBrains. Click on Install, and you will observe that IntelliJ will get installed in the destination folder.Picture13
  6. Click on Finish.Picture14

How to Create a Maven project in IntelliJ

  1. Open IntelliJ IDEA and select New Project.Picture15
  2. Give the project name, location, language (Java), Build system (Maven), and JDK version, and then click on Create.
  3. Picture16A Maven project should be created with the project structure shown below.Picture17

How to Launch Multiple Browsers using the Selenium WebDriver

In Selenium, different webdrivers are used for different browsers: FirefoxDriver for the Firefox browser, ChromeDriver for Google Chrome, and InternetExplorerDriver for Internet Explorer.

Let’s consider the example of launching the Chrome browser.

  1. The very first step is to add the selenium dependency to the pom.xml file. You can get the latest selenium version dependency from: https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-javaPicture18
  2. Now create a Java Class inside the src=>main=>java folder.Picture19
  3. After the Java class gets created, start writing code as mentioned below:Picture20
  4. After writing the code, when you run the Java class, the Chrome browser will launch as shown below.Picture21
  5. To launch the Chrome browser, you need to download the chromedriver.exe file. (https://chromedriver.chromium.org/downloads)
  6. System.setProperty is used to set webdriver.chrome.driver to the chromedriver.exe location (path where chromedriver.exe file is stored)
  7. WebDriver driver = new ChromeDriver(): WebDriver is an interface, and driver is a reference variable instantiated using the ChromeDriver class.
  8. driver.get(URL) will launch the Chrome browser and navigate the browser to the URL.
  9. driver.manage().window().maximize() will maximize the launched browser window.
  10. driver.close() will close the launched browser.

Similarly, you can Launch the Firefox Browser, the Only Change to the Code will be:

  1. You need to download the geckodriver.exe file: System.setProperty(“webdriver.gecko.driver”, “C:\\Program Files\\geckodriver.exe”);
  2. WebDriver driver = new FirefoxDriver();

Picture22

Picture23

Conclusion

By following the above steps, you will be able to successfully install JDK and IntelliJ on your device and can use the concept of Selenium WebDriver to automate test cases in different browsers.





Source link

Social media & sharing icons powered by UltimatelySocial
error

Enjoy Our Website? Please share :) Thank you!