Selenium Introduction
Selenium Introduction
Selenium Features
1. Selenium is a free (open source) automated testing suite for
web applications across different browsers, platforms and
programming languages.
2. Selenium IDE provides a playback and record feature for
authoring tests without the need to learn a test scripting
language.
3. It helps testers to record their actions and export them as a
reusable script with a simple-to-understand and easy-to-use
interface.
4. It also supports parallel test execution which reduces time and
increases the efficiency of tests.
5. Selenium can be integrated with frameworks like Ant and
Maven for source code compilation, and can be integrated
with testing frameworks like TestNG for application testing and
generating reports.
6. Selenium requires fewer resources as compared to other
automation test tools.
7. Selenium web driver does not require server installation, test
scripts interact directly with the browser.
Selenium Limitations
1. Selenium does not support automation testing for desktop
applications.
2. Selenium requires high skill sets in order to automate tests
more effectively.
3. Since Selenium is open source software, you have to rely on
community forums to get your technical issues resolved.
4. We should know at least one of the supported programming
languages to create tests scripts in Selenium WebDriver.
5. Selenium does not have any inbuilt reportingcapability; you
have to rely on plug-ins like JUnit and TestNG for test reports.
6. It is not possible to perform testing on images.
7. No one is responsible for new features usage; they may or
may not work properly.
Selenium IDE
1. Selenium Integrated Development Environment (IDE) is the
simplest framework in the Selenium suite and is the easiest
one to learn.
2. It is a Firefox/Chrome plugin that you can install as easily as
you can with other plugins.
3. However, because of its simplicity, Selenium IDE should only
be used as a prototyping tool.
4. Selenium IDE is implemented as Firefox extension which
provides record and playback functionality on test scripts.
5. It allows testers to export recorded scripts in many languages
like HTML, Java, Ruby, RSpec, Python, C#, JUnit and
TestNG. You can use these exported script in Selenium RC or
Webdriver.
Selenium IDE-Installation
1. Open Selenium official websiteseleniumhq.org
2. Then click on Download Section.
3. In the webpage search for Selenium IDE and click on Chrome
to install Google Chrome plug-in or click on Firefox to install
Firefox Plug-in.
4. Restart you browser, go to the top right corner on your
browser and look for the Selenium IDE icon.
5. Click on that icon to launch Selenium IDE.
Selenium IDE-Features
1. Very easy to use and install.
2. No programming experience is requied.
3. Export tests to different programming languages.
Selenium IDE-Limitations
1. Available in only Firefox and Chrome.
2. Designed only to create prototypes of tests.
3. Test execution is slow.
Selenium Webdriver
1. Selenium WebDriver is the most important component of
Selenium Tool's Suite.
2. The initial version of Selenium i.e Selenium v1 consisted of
only IDE, RC and Grid. Selenium WebDriver was first
introduced as a part of Selenium v2.0. However, with the
release of Selenium v3, RC has been deprecated and moved
to legacy package.
3. In WebDriver, test scripts can be developed using any of the
supported programming languages and can be run directly in
most modern web browsers. Languages supported by
WebDriver include C#, Java, Perl, PHP, Python and Ruby.
4. Selenium Web driver is most popular with Java and C#.
5. Selenium WebDriver performs much faster as compared to
Selenium RC because it makes direct calls to the web
browsers. RC on the other hand needs an RC server to
interact with the browser.
6. Selenium uses drivers, specific to each browser in order to
establish a secure connection with the browser without
revealing the internal logic of browser's functionality.
7. WebDriver is supporting dynamic web pages where elements
of a page may change without the page itself being reloaded.
8. The more pain while doing automation is the handling
Javascripts alerts & prompts. The WebDriver very verse with
handle the Javascript alerts, prompts and handling multiple
frames, multiple browser windows.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//InternetExplorer
System.setProperty("webdriver.ie.driver", "E:\\Softwares\\
IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("http://google.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
//GoogleChrome
System.setProperty("webdriver.chrome.driver", "E:\\
Softwares\\ChromeDriver.exe");
WebDriver driver2 = new ChromeDriver();
driver2.manage().window().maximize();
driver2.get("http://google.com");
System.out.println(driver2.getTitle());
System.out.println(driver2.getCurrentUrl());
//Firefox
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\
geckodriver.exe");
WebDriver driver3 = new InternetExplorerDriver();
driver.manage().window().maximize();
driver3.get("http://google.com");
System.out.println(driver3.getTitle());
System.out.println(driver3.getCurrentUrl());
}
}
1. In selenium, we
use System.setProperty("name","value") method because
the browser doesn’t have a built-in server to run the
automation code so you will need a
Chrome/IE/Gecko(according to requirement) driver server for
communicating your Selenium code to the browser. In this
method we have to pass two parameters, first parameter is
name of the browser which we want to connect and second
parameter is the path of the related driver which we have
downloaded in Step 3.
For example System.setProperty("webdriver.ie.driver",
"E:\\Softwares\\SeleniumDrivers\\IEDriverServer.exe");
2. In the next step we have created a browser object to access
and to perform some operations on that
browser. WebDriver defines common methods which all
browser classes (such as Firefox, Chrome etc.,) use. All these
class methods are derived from WebDriver interface.
For example WebDriver driver = new
InternetExplorerDriver();
3. The WebDriver provides the window interface for setting up
the browser window size, state, and so on. When we call
the maximize() method, the browser window will be
maximized from normal or minimized state.
For example driver.manage().window().maximize();
4. If we want to open a specific URL on that browser we will
use get() method. In that method we have to pass the specific
URL.
For example driver.get("http://google.com");
5. If we want to get current page title we wil
use driver.gettitle() method and if we want to get current
page URL we will use driver.getCurrentUrl() method.
WebElement element =
driver.findElement(By.id("elementId"));
WebElement element =
driver.findElement(By.className("elementsClass"));
WebElement element =
driver.findElement(By.partialLinkText("Click"));
WebElement element =
driver.findElement(By.cssSelector("div#elementId"));
driver.findElement(By.cssSelector("div#elementId")).sendKe
ys(“TestSelenium “);
driver.findElement(By.linkText("Click Here")).click();
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class LocatorsDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://fb.com");
driver.findElement(By.id("email")).sendKeys("9052938526");
driver.findElement(By.name("pass")).sendKeys("password");
driver.findElement(By.className("inputtext")).sendKeys("abhishek
@gmail.com");
driver.findElement(By.tagName("input")).sendKeys("7569645388");
driver.findElement(By.partialLinkText("Forgotten")).click();
driver.findElement(By.linkText("Forgotten Password?")).click();
driver.findElement(By.cssSelector("#u_0_q")).sendKeys("90529385
26");
driver.findElement(By.xpath("//*[@id=\"reg_pages_msg\"]/a")).click()
;
}
}
Browser Commands
get() method: The driver.get() method is used to navigate to a
web page by passing the string URL as parameter.
driver.get("http://facebook.com");
System.out.println(driver.getTitle());
getCurrentUrl() method: In WebDriver, this method fetches
the string representing the Current URL of the current web
page.
System.out.println(driver.getCurrentUrl());
driver.close();
driver.quit();
Navigation Commands
navigate().to() method: The driver.navigate().to() method
does the task of opening a web page like driver.get() method.
driver.navigate().to("http://youtube.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.findElement(By.id("id123")).sendKeys(Keys.F5);
WebElement Commands
click() method: The click() method in Selenium is used to
perform the click operation on web elements.
driver.findElement(By.id("button1")).click();
driver.findElement(By.id("firstname")).sendKeys("Abhishek");
driver.findElement(By.name("surname")).clear();
driver.findElement(By.id("email")).getText();
driver.findElement(By.id("UserName")).isDisplayed();
driver.findElement(By.id("UserName")).isEnabled();
driver.findElement(By.id("Sex-Male")).isSelected();
driver.findElement(By.id("SubmitButton")).submit();
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
driver.findElement(By.xpath("xpath1"));
driver.close();
driver.quit();
driver.navigate().to("https://google.com");
Thread.sleep(10000);
driver.navigate().back();
Thread.sleep(5000);
driver.navigate().forward();
driver.navigate().refresh();
driver.findElement(By.xpath("hello")).sendKeys("9052938526");
Thread.sleep(5000);
driver.findElement(By.xpath("hello")).clear();
System.out.println(driver.findElement(By.id("heading2")).getText());
System.out.println(driver.findElement(By.id("selenium")).isDisplayed
());
System.out.println(driver.findElement(By.id("b2")).isEnabled());
System.out.println(driver.findElement(By.id("male")).isSelected());
}
driver.findElement(By.id("name")).sendKeys("Abhishek");
driver.findElement(By.id("name")).getAttribute("value");
6. We can also get the type of the text box. For this purpose we
use getAttribute("type") method.
driver.findElement(By.id("name")).getAttribute("type");
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
WebElement name = driver.findElement(By.id("name"));
name.sendKeys("Kotha Abhishek");
driver.findElement(By.id("pwd")).sendKeys("Abhishek");
name.clear();
System.out.println(name.getAttribute("value"));
System.out.println(name.getAttribute("type"));
driver.switchTo().alert().accept();
driver.switchTo().alert().dismiss();
driver.switchTo().alert().getText();
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
driver.findElement(By.id("file")).click();
driver.findElement(By.id("file")).sendKeys("C:\\Users\\user\\
Desktop\\Selenium.html");
Thread.sleep(3000);
driver.switchTo().alert().accept();
driver.switchTo().alert().dismiss();
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().sendKeys("9052938526");
driver.switchTo().alert().accept();
dropdown.selectByIndex(5);
dropdown.selectByValue("India");
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
Select dropdown1 = new
Select(driver.findElement(By.name("country")));
dropdown1.selectByVisibleText("India");
dropdown1.selectByIndex(5);
dropdown1.selectByValue("Australia");
List<WebElement> list1 = dropdown1.getOptions();
int size=list1.size();
System.out.println(size);
for(int i =0; i<size ; i++){
String optValue = list1.get(i).getText();
System.out.println(optValue);
}
}
}
WebElement
htmltable=driver.findElement(By.xpath("//*[@id='main']/table[1]
/tbody"));
5. Get all the rows with tag name ‘tr’ and store all the elements in
a list of web elements. Now all the elements with tag ‘tr’ are
stored in ‘rows’ list.
List<WebElement>
rows=htmltable.findElements(By.tagName("tr"));
6. Loop through each row and get the list of elements with tag
‘th’.
List<WebElement>
columns=rows.get(rnum).findElements(By.tagName("th"));
System.out.println("Number of columns:"+columns.size());
System.out.println(columns.get(cnum).getText());
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
WebElement table =
driver.findElement(By.xpath("/html/body/table"));
List <WebElement> rows =
table.findElements(By.xpath("/html/body/table/tbody/tr"));
int rowcount = rows.size();
System.out.print(table.findElement(By.xpath("/html/body/table/tbody
/tr["+i+"]/th["+j+"]")).getText()+" ");
}
System.out.println();
}
for(int i=2; i<=rowcount; i++){
for(int j=1; j<=columncount; j++) {
System.out.print(table.findElement(By.xpath("/html/body/table/tbody
/tr["+i+"]/td["+j+"]")).getText()+" ");
}
System.out.println();
}
}
}
driver.switchTo().frame(0);
driver.switchTo().frame("iframe1");
9. Switch back to the Main Frame : To move back to the parent
frame, you can either use switchTo().parentFrame() or if you
want to get back to the main (or most parent) frame, you can
use switchTo().defaultContent();
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
driver.switchTo().frame("seleniumframe");
String heading =
driver.findElement(By.xpath("//*[@id=\"1\"]")).getText();
System.out.println(heading);
driver.switchTo().parentFrame();
driver.switchTo().frame(1);
String heading2 =
driver.findElement(By.xpath("//*[@id=\"1\"]")).getText();
System.out.println(heading2);
int framecount =
driver.findElements(By.tagName("iframe")).size();
System.out.println(framecount);
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
// options.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
class Selenium {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","E:\\Drivers\\
chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
Collections.sort(tempList);
if(ifSortedAscending)
{
System.out.println("List is sorted");
}
else
System.out.println("List is not sorted.");
driver.quit();
}
}
Handle Multiple Windows/Tabs in Selenium
1. On an HTML page, a link can open up in a new window/tab.
2. There is only one way you can get multiple windows via
Selenium web driver, that is by clicking on a link that opens
the page in a new browser window.
3. Selenium web driver keeps a track of how many windows it
opened during a session.
4. Window handle is a unique string value that uniquely identifies
a Browser window on desktop. It is guaranteed that each
browser will have a unique window handle.
5. To get Window handle WebDriver interface provides two
methods – getWindowHandle(), getWindowHandles().
6. getWindowHandle() : method return a string value and it
returns the Window handle of current focused browser
window.
7. getWindowHandles() : method returns a set of all Window
handles of all the browsers that were opened in the session.
8. There is a concept of current focused window which means
that all selenium webdriver commands will go to the focused
window. By default the focus is always on the Parent window.
9. WebDriver.SwitchTo().window(String
windowHandle) : This command takes in a window handle
and switches the driver context on that window. Once the
Switch happens all the driver commands will go to the newly
focused window. This is very important to understand, without
switching to the desired window we wil not be able to perform
any action on that window.
10. WebDriver.Close() : command will close the current
window on which the focus is present. This can be used to
close windows selectively. Just switch to the window that you
want to close by using the correct Window handle and the call
the WebDriver.close command.
11. WebDriver.quit() : command will close all the windows
opened in the session. This command basically shuts down
the driver instance and any further commands to WebDriver
results in exception.
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
driver.findElement(By.xpath("//*[@id=\'c\']")).click();
driver.findElement(By.xpath("//*[@id=\'java\']")).click();
driver.findElement(By.xpath("//*[@id=\'python\']")).click();
driver.switchTo().window(ids[2]);
System.out.println("2nd Child window Title : "+driver.getTitle());
driver.switchTo().window(ids[3]);
System.out.println("3rd Child window Title : "+driver.getTitle());
driver.switchTo().window(ids[1]);
System.out.println("1st Child window Title : "+driver.getTitle());
driver.switchTo().window(ids[0]);
System.out.println("Parent window Title : "+driver.getTitle());
driver.quit();
}
}
Mouse Click & Keyboard Event: Actions
Class
1. Actions class is an ability provided by Selenium for handling
keyboard and mouse events.
2. In Selenium WebDriver, handling these events includes
operations such as drag and drop, clicking on multiple
elements with the control key.
3. The following are the most commonly used keyboard and
mouse events provided by the Actions class.
4. clickAndHold() : Clicks (without releasing) at the current
mouse location.
5. contextClick() : Performs a context-click at the current mouse
locationPerforms a key release.. (Right Click Mouse Action).
6. doubleClick() : Performs a double-click at the current mouse
location.
7. dragAndDrop(source, target) : Performs click-and-hold at
the location of the source element, moves to the location of
the target element, then releases the mouse.
8. keyDown(modifier_key) : Performs a modifier key press.
Does not release the modifier key - subsequent interactions
may assume it's kept pressed. (Keys.ALT, Keys.SHIFT, or
Keys.CONTROL).
9. keyUp(modifier _key) : Performs a key release.
10. moveToElement(toElement) : Moves the mouse to the
middle of the element.
11. release() : Releases the depressed left mouse button at
the current mouse location.
12. Instantiate a new Actions object.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
class Selenium {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
WebElement source =
driver.findElement(By.xpath("//*[@id=\'box6\']"));
WebElement destination =
driver.findElement(By.xpath("//*[@id=\'box106\']"));
WebElement element3 =
driver.findElement(By.xpath("//*[@id=\'header\']/nav/div/div[2]/ul/
li[6]/ul/li[1]/ul/li[2]/a"));
act.moveToElement(element1).build().perform();
act.moveToElement(element2).build().perform();
act.moveToElement(element3).click().build().perform();
act.moveToElement(element1).moveToElement(element2).moveTo
Element(element3).click().build().perform();
act.clickAndHold(source).moveToElement(destination).release().bui
ld().perform();
WebElement source2 =
driver.findElement(By.xpath("//*[@id=\'box1\']"));
WebElement destination2 =
driver.findElement(By.xpath("//*[@id=\'box101\']"));
act.dragAndDrop(source, destination).build().perform();
act.clickAndHold(source2).moveToElement(destination2).release().
build().perform();
Thread.sleep(5000);
WebElement button =
driver.findElement(By.xpath("/html/body/div/section/div/div/div/p/
span"));
act.contextClick(button).build().perform();
act.moveToElement(driver.findElement(By.xpath("/html/body/ul/li[5]/
span"))).click().build().perform();
}
}
List<WebElement> links =
driver.findElements(By.tagName("a"));
Iterator it = links.iterator();
15. Now the most important part is to check in the links are
working. Here I will introduce to you a Class from Java,
called HttpURLConnection class. This class is used to make
HTTP requests to the webserver hosting the links extracted
16. Consider an air ticket booking application. The color of
booked and available seats are different. Red represents the
booked seats, and available seats are represented by green.
So, for verifying whether a seat is booked or available, QAs
need to fetch the attribute (color) value through the test script.
Once the status of the seat is verified, only then can QAs
verify further test scenarios.
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException,
IOException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com/");
List<WebElement> links =
driver.findElements(By.tagName("a"));
System.out.println(links.size());
if(rescode>=400) {
System.out.println(url + "---> is broken link");
}else {
System.out.println(url + "---> is valid link");
}
}
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://demo.automationtesting.in/FileDownload.html");
driver.findElement(By.xpath("//*[@id=\"textbox\"]")).sendKeys("Testi
ng text file");
driver.findElement(By.xpath("//*[@id=\"createTxt\"]")).click();
driver.findElement(By.xpath("//*[@id=\"link-to-
download\"]")).click();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf");
profile.setPreference("browser.download.manager.showWhenStarti
ng", false);
profile.setPreference("pdfjs.disabled", true);
System.setProperty("webdriver.gecko.driver", "E:\\Drivers\\
geckodriver.exe");
WebDriver driver = new FirefoxDriver(option);
driver.manage().window().maximize();
driver.get("http://demo.automationtesting.in/FileDownload.html");
driver.findElement(By.xpath("//*[@id=\"textbox\"]")).sendKeys("Testi
ng text file");
driver.findElement(By.xpath("//*[@id=\"createTxt\"]")).click();
driver.findElement(By.xpath("//*[@id=\"link-to-
download\"]")).click();
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
//
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Abh
ishek");
//
driver.findElement(By.xpath("//input[@placeholder='Name']")).send
Keys("Abhishek");
// driver.findElement(By.xpath("//button[text()='Login with
Google']")).click();
//driver.findElement(By.xpath("button[contains(text(),'kjasg')]")).click
();
//driver.findElement(By.xpath("button[starts-
with(text(),'kjasg')]")).click();
//driver.findElement(By.xpath("button[ends-
with(text(),'kjasg')]")).click();
//input[@id='login']/following-sibling::button
//input[@id='login']/preceding-sibling::button
//input[@id='login']/parent::form
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
class Selenium {
public static void main(String[] args) throws FindFailed {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
act.click(driver.findElement(By.xpath("//*[@id=\'file\']"))).build().perfo
rm();
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
class Selenium {
public static void main(String[] args) throws FindFailed {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
sc.click(p1);
sc.click(p2);
sc.click(p3);
}
}
Read Data from MS-Excel in selenium
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class Selenium {
public static void main(String[] args) throws IOException {
// System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
// WebDriver driver = new ChromeDriver();
// driver.manage().window().maximize();
// driver.get("file:///C:/Users/user/Desktop/Selenium.html");
XSSFSheet s = wb.getSheetAt(0);
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://localhost:8084/KothaAbhishekProject/");
driver.findElement(By.xpath("//input[@name='name']")).sendKeys(c
ell1);
driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(cel
l2);
driver.findElement(By.xpath("//input[@name='email']")).sendKeys(c
ell3);
driver.findElement(By.xpath("//input[@name='number']")).sendKeys
(String.valueOf(cell4));
//
driver.findElement(By.xpath("//input[@name='gender']")).sendKeys(
cell5).click();
if(val.equalsIgnoreCase(cell5)) {
radiobutton.get(j).click();
break;
}
}
driver.findElement(By.xpath("//input[@name='Register']")).click();
driver.get("http://localhost:8084/KothaAbhishekProject/");
}
driver.close();
workbook.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws IOException{
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://localhost:8084/PracticeApplication/");
driver.findElement(By.xpath("//input[@name='name']")).sendKeys(u
name);
driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(up
assword);
driver.findElement(By.xpath("//input[@name='Login']")).click();
String message=
driver.findElement(By.xpath("//span[@class='error']")).getText();
if(message.equalsIgnoreCase("Valid User")) {
sheet.getRow(i).createCell(2).setCellValue("Pass");
}else {
sheet.getRow(i).createCell(2).setCellValue("Fail");
}
workbook.write(fout);
driver.get("http://localhost:8084/PracticeApplication/");
}
workbook.close();
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
class Selenium {
public static void main(String[] args) throws AWTException,
InterruptedException{
System.setProperty("webdriver.gecko.driver", "E:\\Drivers\\
geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://demo.automationtesting.in/FileDownload.html");
driver.findElement(By.xpath("//*[@id=\"textbox\"]")).sendKeys("Koth
a Abhishek");
driver.findElement(By.xpath("//*[@id=\"createTxt\"]")).click();
driver.findElement(By.xpath("//*[@id=\"link-to-
download\"]")).click();
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
class Selenium {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/user/Desktop/Selenium.html");
act.click(driver.findElement(By.xpath("//*[@id=\"upload\"]"))).build().
perform();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(select
, null); //ctrl+c
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V); //ctrl+v
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/datepicker/");
driver.findElement(By.xpath("//*[@id=\'datepicker\']")).click();
String month = driver.findElement(By.xpath("//*[@id=\'ui-
datepicker-div\']/div/div/span[1]")).getText();
String year = driver.findElement(By.xpath("//*[@id=\'ui-
datepicker-div\']/div/div/span[2]")).getText();
int rows = driver.findElements(By.xpath("//table[@class='ui-
datepicker-calendar']/tbody/tr")).size();
int cols=7;
boolean flag=false;
while(true) {
if(month.equals(arr[1]) && year.equals(arr[2])) {
for(int i=1; i<=rows; i++) { // rows tr
for(int j=1; j<=cols; j++) { // columns
String day =
driver.findElement(By.xpath("//table[@class='ui-datepicker-
calendar']/tbody/tr["+i+"]/td["+j+"]")).getText();
if(day.equals(arr[0])) {
driver.findElement(By.xpath("//table[@class='ui-datepicker-
calendar']/tbody/tr["+i+"]/td["+j+"]")).click();
flag=true;
break; // columns stop (inner for loop)
}
}
if(flag) { // stops rows
break; // rows stops (outer for loop)
}
}
if(flag) {
System.out.println("success");
break; // while loop stop
}else {
System.out.println("Please Enter Correct Date");
driver.close();
break;
}
}else {
driver.findElement(By.xpath("//*[@id=\'ui-datepicker-div\']/div/a[2]/
span")).click();
month = driver.findElement(By.xpath("//*[@id=\'ui-datepicker-
div\']/div/div/span[1]")).getText();
year = driver.findElement(By.xpath("//*[@id=\'ui-datepicker-
div\']/div/div/span[2]")).getText();
rows = driver.findElements(By.xpath("//table[@class='ui-
datepicker-calendar']/tbody/tr")).size();
}
}
}
}
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class Selenium {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/datepicker/#other-months");
driver.findElement(By.xpath("//*[@id=\'datepicker\']")).click();
while(true) {
if(month.equals(arr[1]) && year.equals(arr[2])) {
if(day.equals(arr[0])) {
currentday.click();
flag=true;
break; // stops for loop
}
}
if(flag) {
System.out.println("Success");
break; // stops while loop
}else {
System.out.println("Please Enter Valid Date");
driver.close();
break; // stops while loop
}
}else {
driver.findElement(By.xpath("//*[@id=\'ui-datepicker-div\']/div/a[2]/
span")).click();
month = driver.findElement(By.xpath("//*[@id=\'ui-datepicker-
div\']/div/div/span[1]")).getText();
year = driver.findElement(By.xpath("//*[@id=\'ui-datepicker-
div\']/div/div/span[2]")).getText();
}
}
}
}
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
class Selenium {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\\Drivers\\
chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/datepicker/#dropdown-month-
year");
String date = "29-Feb-2024"; //My birthday
String arr[] = date.split("-"); // index 0
driver.findElement(By.xpath("//*[@id=\'datepicker\']")).click();
month.selectByVisibleText(arr[1]);
year.selectByVisibleText(arr[2]);
boolean flag=false;
if(day.equals(arr[0])) {
currentday.click();
flag=true;
break;
}
}
if(flag) {
System.out.println("Success");
}else {
driver.close();
System.out.println("Please Enter a Valid Date");
}
}