1.Install selenium ide
Download latest
version of “Eclipse
IDE for Java Developers”
Be sure to choose correctly between Windows 32 Bit and 64 Bit versions.
You should be able
to download an exe file named “eclipse-inst-win64” for Setup
Double-click on file to Install the Eclipse. A new window will open. Click Eclipse IDE for Java Developers.
After that, a new
window will open which click button marked 1 and change path to “C:\eclipse”.
Post that Click on Install button marked 2
After successful
completion of the installation procedure, a window will appear. On that window
click on Launch
This will start
eclipse neon IDE for you.
Step 3 – Download the Selenium Java Client Driver
You can download Selenium Webdriver for Java Client
Driver here. You
will find client drivers for other languages there, but only choose the one for
Java.
This download
comes as a ZIP file named “selenium-3.14.0.zip”. For simplicity of Selenium
installation on Windows 10, extract the contents of this ZIP file on your C
drive so that you would have the directory “C:\selenium-3.14.0\”. This
directory contains all the JAR files that we would later import on Eclipse for
Selenium setup.
Step 4 – Configure
Eclipse IDE with WebDriver
1.
Launch the “eclipse.exe” file inside the “eclipse” folder that
we extracted in step 2. If you followed step 2 correctly, the executable should
be located on C:\eclipse\eclipse.exe.
2.
When asked to select for a workspace, just accept the default
location
3. Create a new project through File > New > Java Project.
Name the project as “newproject”.
A new pop-up window will open enter details as follow
1.
Project Name
2.
Location to save project
3.
Select an execution JRE
4.
Select layout project option
5.
Click on Finish button
4. In this step,
1.
Right-click on the newly created project and
2.
Select New > Package, and name that package as “newpackage”.
A pop-up window will open to name the package,
1.
Enter the name of the package
2.
Click on Finish button
5. Create a new Java class under newpackage by right-clicking on
it and then selecting- New > Class, and then name it as “MyClass”. Your
Eclipse IDE should look like the image below.
When you click on Class, a pop-up window will open, enter
details as
1.
Name of the class
2.
Click on Finish button
This is how it
looks like after creating class.
Now selenium WebDriver’s into Java Build Path
In this step,
1.
Right-click on “newproject” and select Properties.
2.
On the Properties dialog, click on “Java Build Path”.
3.
Click on the Libraries tab, and then
4.
Click on “Add External JARs..”
When you click on
“Add External JARs..” It will open a pop-up window. Select the JAR files you want
to add.
After selecting jar files, click on OK button.
Select all files inside the lib folder
Select files
outside lib folder
Once done, click
“Apply and Close” button
6. Add all the JAR files inside and outside the “libs” folder.
Your Properties dialog should now look similar to the image below.
6.
Finally,
click OK and we are done importing Selenium libraries into our project.
What is Alert in
Selenium?
An Alert in Selenium is
a small message box which appears on screen to give the user some information
or notification. It notifies the user with some specific information or error,
asks for permission to perform certain tasks and it also provides warning
messages as well.
Here are few alert in
Selenium types:
1) Simple Alert
The simple alert class
in Selenium displays some information or warning on the screen.
2) Prompt Alert.
This Prompt Alert asks some input from the user and Selenium
webdriver can enter the text using sendkeys(” input…. “).
3) Confirmation Alert.
This confirmation alert asks permission to do some type of
operation.
How to handle Alert in
Selenium WebDriver
Alert interface
provides the below few methods which are widely used in Selenium
Webdriver.
1) void dismiss() // To click on the ‘Cancel’
button of the alert.
driver.switchTo().alert().dismiss();
2) void accept() // To click on the ‘OK’ button
of the alert.
driver.switchTo().alert().accept();
3) String getText() // To capture the alert
message.
driver.switchTo().alert().getText();
4) void sendKeys(String stringToSend) // To send some
data to alert box.
driver.switchTo().alert().sendKeys("Text");
You can see a number of Alert methods are
displayed as shown in below screen suggested by Eclipse.
We can easily switch to alert from the main window by using
Selenium’s .switchTo() method.
Now we automate the given below scenario.
In this scenario, we will use Guru99 demo site to illustrate
Selenium Alert handling.
Step 1) Launch the web
browser and open the site http://demo.guru99.com/test/delete_customer.php
Step 2) Enter Any Customer
id.
Step
3) After
entering the customer ID, Click on the “Submit” button
Step
4) Reject/accept
the alert.
Handling Alert in Selenium Webdriver using above scenario
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.openqa.selenium.NoAlertPresentException;
import
org.openqa.selenium.Alert;
public class AlertDemo {
public static void main(String[] args) throws
NoAlertPresentException,InterruptedException
{
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Alert Message handling
driver.get("http://demo.guru99.com/test/delete_customer.php");
driver.findElement(By.name("cusid")).sendKeys("53920");
driver.findElement(By.name("submit")).submit();
// Switching to Alert
Alert alert =
driver.switchTo().alert();
// Capturing alert message.
String alertMessage=
driver.switchTo().alert().getText();
// Displaying alert message
System.out.println(alertMessage);
Thread.sleep(5000);
// Accepting alert
alert.accept();
}
}
Output :
When you execute the above code, it launches the site. Try to
delete Customer ID by handling confirmation alert that displays on the screen,
and thereby deleting customer id from the application.
Comments