Setting Up Selenium With IntelliJ
For automating the checking of websites there is no product I like better than Selenium. Why? Because it's free, can be used with multiple languages, and is easy to set-up. It has it's down sides - tests can be flaky - but this is not unique to Selenium, any browser test tool experiences these issues. Once you become familiar you can learn ways to work around the issues of race conditions and flaky tests.
Here I will walk through the steps to setting up IntelliJ to use Selenium with JUnit. In this instance we will be using Chrome browser on a windows machine.
These are the things you will need:
IntelliJIDE
JavaJDK
Chrome browser
Selenium java library
JUnit library
chromedriver
1- Download IntelliJ IDEcommunity version https://www.jetbrains.com/idea/download/download-thanks.html?platform=windows&code=IIC
2- Download Selenium library https://www.seleniumhq.org/download/
14- Add a new java class to the IntelliJ project and call it OpenGoogle (Right click on the src folder in the project and select new>Java Class)
15- Paste the following code in to the class:
16- Right click on the class in the left side panel and select 'Run Open Google'
Here I will walk through the steps to setting up IntelliJ to use Selenium with JUnit. In this instance we will be using Chrome browser on a windows machine.
These are the things you will need:
IntelliJIDE
JavaJDK
Chrome browser
Selenium java library
JUnit library
chromedriver
1- Download IntelliJ IDEcommunity version https://www.jetbrains.com/idea/download/download-thanks.html?platform=windows&code=IIC
2- Download Selenium library https://www.seleniumhq.org/download/
3- Download Chrome Driver library https://sites.google.com/a/chromium.org/chromedriver/
4- Download a JDK if you dont already have one https://www.oracle.com/technetwork/java/javase/downloads/index.html
5- Download the Junit jar https://github.com/junit-team/junit4/wiki/Download-and-Install
6- Download chrome browser
7- Create a folder on your c drive called 'Selenium'
8- Unzip the chromedriver exe and place in the Selenium folder
9- Add the chromedriver to path variables ;C:\Selenium\chromedriver.exe (instructions can be found on the internet for adding to path variables)
10- Install the JDK
11- Install IntelliJ
12- Create a new Java project in IntelliJ (File>New>Project>Java)
13- Add the external Selenium & Junit libraries in intelliJ Modules (File>Project Structure>Modules>+>JARs or Directories14- Add a new java class to the IntelliJ project and call it OpenGoogle (Right click on the src folder in the project and select new>Java Class)
15- Paste the following code in to the class:
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertTrue; public class OpenGoogle { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { baseUrl = "https://www.google.com"; } @Test public void testGoogleSearch() throws Exception { // Optional, if not specified, WebDriver will search path for chromedriver.
System.setProperty("webdriver.chrome.driver", "C:/Tools/Selenium/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts(). implicitlyWait(5, TimeUnit.SECONDS);
/* GO TO GOOGLE */
driver.get("https://www.google.com"); System.out.println("Loaded Google"); /* ENTER SEARCH TERM*/
driver.findElement(By.name("q")).click(); driver.findElement(By.name("q" )).sendKeys("https:// talksoftwaretesting.blogspot. com/"); WebElement textbox = driver.findElement(By.name("q" )); textbox.sendKeys(Keys.ENTER); System.out.println("Searched for Blog"); /* CHECK RESULT IS DISPLAYED */
assertTrue(driver.getPageSource().contains("Talk Software Testing by Dennean Cooper")); if (driver.getPageSource(). contains("Talk Software Testing by Dennean Cooper")) { System.out.println("Talk Software Testing Blog found"); } else { System.out.println("Could not find blog in results"); } } @After public void tearDown() throws Exception { WebDriver driver; // driver.quit(); } }
16- Right click on the class in the left side panel and select 'Run Open Google'
Comments
Post a Comment