Writing your first script for Selenium 4 (Alpha) in Java
Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Selenium WebDriver allows you to choose a programming language of your choice to create test scripts.
In this article, we will be discussing setting up Selenium 4 (Alpha) in Java using Eclipse IDE.
Setup
- Download Java here and install on your machine
- After you have installed Java, go to your command / terminal and type this
java -version
If you see the version of Java , it means that Java is installed successfully. If not, check that you have set up the environment path in this link.
3. Download Eclipse here . Ensure that you select the installation with Maven.
Eclipse setup
- Launch Eclipse
- Go to Help and Install New Software. See image
3. Type "https://dl.bintray.com/testng-team/testng-eclipse-release/" in Work With input field and Add. You should see something like this
4. Checked the TestNG and Add. A dialog box will pop up. Enter "TestNG" in Name Text Field and "https://beust.com/eclipse" in the location. Install TestNG.
If you have any reason that fail to install TestNG, go to this link and look for method 3.
Now we have done the setup for Eclipse and Java, the next step is to setup for the project
Setting up the Project
- In Eclipse, create a new Project , Other and Maven Project
2. Enter a folder location , your Group Id and Artifact Id. Click Next and you are done.
You have created your first project
3. Create a class. Go to File, New , Others . Select TestNG class. Name it anything you want. I call it "HelloWorld". In the XML suite file, input "testng.xml". Click Finish and you are done creating your very first file.
4. Open your new file HelloWorld.java. It should be under your folder or package. Input this under "public void f()"
System.out.println ("Hello World!");
It should look something like this
5. Hover your mouse to the menu bar "Play" button (green button with play arrow) and click it.
You will see in the console that it indicate a "PASSED f". Congrats. you have written your first Java !
We will move on to next section and start setting up for Selenium
Setting up POM.xml
POM is an acronym for Project Object Model. The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom. xml file, then executes the goal.. This is where you can add Selenium , Surefire and TestNG.Pom
Adding Surefire
Surefire allows you to run your script from command prompt. Running from command prompt opens an unlimited possibility in executing automation. Here is the link to copy the settings to your POM.xml under "<plugins>"
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <suiteXmlFiles> <!-- TestNG suite XML files --> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> <version>3.0.0-M4</version>
</plugin>
TestNG
Add TestNG in the POM.xml will allow the test to execute TestNG. The link is in here . Copy this setting in POM.xml under "<dependencies>"
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope>
</dependency>
Selenium 4 (Alpha)
Selenium will need to be added in POM.xml under dependency. As mention, this will allow you to run scripts to test web sites. You can copy the settings in here
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.0.0-alpha-5</version>
</dependency>
First Script
- Chromedriver path
We are going to write a script to launch Chrome browser and input text in the search field in Google Search (https://www.google.com") Let's configurate the script first.
Find out where you have install your Chromedriver. Chromedriver is needed for Selenium to interact with the Chrome browser. If you have not, go to here to install the Chromedriver. Make sure you install the right version as different Chrome browser version require different Chromedriver version.
Once you have the path of your Chromedriver, input this like this under HelloWorld
private String chromePath = "/Users/ivantay/WebDrivers/chrome/mac/chromedriver";
2. Initialize WebDriver
You will need to initialize WebDriver. I named this variable as driver and call the WebDriver to point to the ChromePath. It looks like this.
The script will look for "@BeforeTest" first and run the 2 lines- It will setup the Chromedriver using "driver" as variable. We are now ready to launch our Chrome browser.
3. Launch the browser
Declare the string for the url. It will be "https://www.google.com"
private String url = "https://www.google.com/";
Now under the method "f()" we can type this to launch the browser
driver.get(url);
Driving Sustainable Innovation | Green Finance | ex-Sembcorp | Carbon Accounting | Lecturer, Mentor | Digital Transformation | Seeking Impactful Opportunities in Sustainability
4 年I have started to learn some python programming recently. Very interesting, while loop, if loop, all these stuff. Ivan T.
| Commercial and Business Development | Sustainability Advocate | Interior Designing | Hospitality | Project Management |
4 年Thanks for the sharing Ivan Is it difficult to start learning Python ?
I help people discover the best version of themselves...Younger, Healthier, Happier | Wellness Consultant
4 年Selenium seeing Selenium in Java ?? ?? Ivan T.
Regional head of Supply Chain on commodity trading, specialised in crisis management and organisational change.
4 年Thanks for your generosity to share Ivan T. have a great day
| Project Manager | PMP? | PMI-ACP? | PMI (SG Chapter) Board Member | Scrum Master | ITIL? | DevOps? | AWS | QMS | 6σ |
4 年Very interesting post on Selenium Ivan T.