driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Opens Free Time Learn homepage in the newly opened window
driver.navigate().to("https://www.freetimelearning.com/");
driver.get("https://www.google.com/");
// Opens a new tab in existing window
driver.switchTo().newWindow(WindowType.TAB);
// Opens Free Time Learn homepage in the newly opened tab
driver.navigate().to("https://www.freetimelearning.com/");
click(WebElement)
: This method is added to Actions class to replace the moveToElement(onElement).click()
. It is used to click on a certain web element.clickAndHold(WebElement)
: This method will replace the moveToElement(onElement).clickAndHold()
. It is used to click on an element without releasing the click.contextClick(WebElement)
: This method will replace moveToElement(onElement).contextClick()
. It will perform the right click operation.doubleClick(WebElement)
: This method is added to replace moveToElement(element).doubleClick()
. It will perform a double click on an element.release()
: This method (user for releasing the pressed mouse button) was initially a part of org.openqa.selenium.interactions.ButtonReleaseAction
class. Now with the updated version of Selenium, it has been moved to Actions class.//html/body/tag1[index]/tag2[index]/.../tagN[index]
//html/body/div[2]/div/div[2]/div/div/div/fieldset/form/div[1]/input[1]
//htmltag[@attribute1='value1' and @attribute2='value2']
//input[@id='passwd' and @placeholder='password']
findElement() | findElements() |
---|---|
The first web element that matches the locator is returned. | It gives you a list of all the web items that match the locator. |
If there are no matching web elements, a NoSuchElementException is produced. | If there are no matching elements, an empty list is returned. |
Syntax − WebElement button = webdriver.findElement(By.name("<<Name value>>")); |
Syntax − List<WebElement> buttons = webdriver.findElements(By.name("<<Name value>>")); |
findElement()
method.driver.findElement(By.id(“user”));
driver.findElement(By.linkText(“Today’s deals”)).click();
driver.findElement(By.linkText(“Service”)).click();
driver.findElement(By.name(“books”).click());
driver.findElement(By.tagName(“button”).click());
driver.findElement(By.className(“inputtext”));
findElement()
method.driver.findElement(By.xpath(“//span[contains(text(),’an account’)]”)).getText();
driver.findElement(By.cssSelector(“input#email”)).sendKeys(“myemail@email.com”);
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(WebDriver Reference, TimeOut);
Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class);
driver.close()
command closes the currently active window on which the user is working or the window being currently accessed by the web driver.driver.quit()
command, unlike the driver.close()
command it closes all the windows opened by the program and hence should be used with care.navigate().back()
: This command is used for taking the user to the last webpage of the browser history.navigate().forward()
: This command is used for taking the user to the next web page of the browser history.navigate().refresh()
: This command is used for reloading the web components of a webpage by refreshing it.navigate().to()
: This command is used for navigating to a particular URL in a new web browser. It takes the URL to be migrated to, as a parameter.Feature | Selenium IDE | Selenium RC | WebDriver |
---|---|---|---|
Browser Compatibility | Selenium IDE comes as a Firefox plugin, thus it supports only Firefox | Selenium RC supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera. | WebDriver supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera. Also supports HtmlUnitDriver which is a GUI less or headless browser. |
Record and Playback | Selenium IDE supports record and playback feature | Selenium RC doesn't supports record and playback feature. | WebDriver doesn't support record and playback feature |
Server Requirement | Selenium IDE doesn't require any server to be started before executing the test scripts | Selenium RC requires server to be started before executing the test scripts. | WebDriver doesn't require any server to be started before executing the test scripts |
Architecture | Selenium IDE is a Javascript based framework | Selenium RC is a JavaScript based Framework. | WebDriver uses the browser's native compatibility to automation |
Object Oriented | Selenium IDE is not an object oriented tool | Selenium RC is semi object oriented tool. | WebDriver is a purely object oriented tool |
Dynamic Finders (for locating web elements on a webpage) |
Selenium IDE doesn't support dynamic finders | Selenium RC doesn't support dynamic finders. | WebDriver supports dynamic finders |
Handling Alerts, Navigations, Dropdowns | Selenium IDE doesn't explicitly provides aids to handle alerts, navigations, dropdowns | Selenium RC doesn't explicitly provides aids to handle alerts, navigations, dropdowns. | WebDriver offers a wide range of utilities and classes that helps in handling alerts, navigations, and dropdowns efficiently and effectively. |
WAP (iPhone/Android) Testing | Selenium IDE doesn't support testing of iPhone/Andriod applications | Selenium RC doesn't support testing of iPhone/Android applications. | WebDriver is designed in a way to efficiently support testing of iPhone/Android applications. The tool comes with a large range of drivers for WAP based testing. For example, AndroidDriver, iPhoneDriver |
Listener Support | Selenium IDE doesn't support listeners | Selenium RC doesn't support listeners. | WebDriver supports the implementation of Listeners |
Speed | Selenium IDE is fast as it is plugged in with the web-browser that launches the test. Thus, the IDE and browser communicates directly | Selenium RC is slower than WebDriver as it doesn't communicates directly with the browser; rather it sends selenese commands over to Selenium Core which in turn communicates with the browser. | WebDriver communicates directly with the web browsers. Thus making it much faster. |
WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);
isDisplayed()
: boolean buttonPresence = driver.findElement(By.id(“gbqfba”)).isDisplayed();
isSelected()
: boolean buttonSelected = driver.findElement(By.id(“gbqfba”)).isSelected();
isEnabled()
: boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
getwindowhandles()
: This is used to get the address of all the open browsers and returns the data type of Set<String>
.getwindowhandle()
: This is used to get the address of the current browser window where it’s focused on and returns the data type of String.Thread.sleep()
: The current thread will stop for a specified period of time. It only waits once when the command is given. It takes only a single argument that is Integer format.set sleep()
: This command will stop the execution for every selenium command. This command is used only for demonstration purposes and also for a slow web application.Feature | Selenium | Quick Test Professional (QTP) |
---|---|---|
Browser Compatibility | Almost all popular browsers are supported. Firefox, Chrome, Safari, Internet Explorer, Edge, etc. | This supports only Chrome, Firefox, and Internet explorer. Also, it supports only the Windows operating system. |
Distribution | Distributed as open-source and it is freely available. | Distributed as a licensed tool and is commercialized. |
Application Under Test (AUT) | Web-based applications are only supported in this kind of automated testing. | This provides support for both webs- based as well as windows applications for automated testing. |
Object Repository | The separate entity in Selenium is used to create the object repository. | Automatic creation of the Object repository happens and is maintained too |
Language Support | Multiple Programming languages like Java, Python, Perl, Ruby, C#, etc., are supported by this Selenium. | Only VBScript language is supported by this QTP. |
Vendor Support | As we already know that Selenium is a free tool, we do not get vendor support in troubleshooting the issues we face. | Vendor support is guaranteed for the users using the QTP and they do get quick responses as QTP is commercialized and licensed. |
Features | Selenium | HP QTP |
TestComplete |
---|---|---|---|
License | Open Source | Required | Required |
Cost | Free | High | High |
Customer support | Yes; Open source community | Yes | Yes |
Release Cycles/ Development Sprints | Smaller release cycles with immediate feedback | Smaller release cycles | Agility only |
Coding skills | Very High | Low | High |
Environment support | Windows, Linux, Mac | Only Windows | Windows only (7, Vista, Server 2008 or later OS) |
Language support | Language support | VB Script | VB Script, JS Script, Delphi Script, C++ & C# |
Method | Description |
---|---|
clickAndHold() | Clicks (without releasing) the current mouse location. |
dragAndDrop() | Performs click-and-hold at the location of the source element, moves. |
source, target() | Moves to the location of the target element, then releases the mouse. |
KeyPress()
: This method is called when you want to press any key.KeyRelease()
: This method is used to release the pressed key on the keyboard.MouseMove()
: This method is called when you want to move the mouse pointer in the X and Y co-ordinates.MousePress()
: This is used to press the left button of the mouse.MouseMove()
: This method helps in releasing the pressed button of the mouse.keys.ALT
or keys.SHIFT
or keys.CONTROL
functions.keyDown(modifier_key)
and keyUp(modifier_key)
Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
public static void main(String[] args)
{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();
driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);
Actions builder = new Actions(driver);
Action seriesOfActions = builder
.moveToElement(txtUerName)
.click()
.keyDown(txtUserName, Keys.SHIFT)
.sendKeys(txtUserName, “hello”)
.keyUp(txtUserName, Keys.SHIFT)
.doubleClick(txtUserName);
.contextClick();
.build();
seriesOfActions.perform();
}
testng.xml
file and sending them as parameters to the test class. This technique is especially useful when we need to pass multiple login credentials of various test environments. Take a look at the code below, in which “myName
” is annotated as a parameter.public class ParameterizedTest1{
@Test
@Parameters("myName")
public void parameterTest(String myName) {
System.out.println("Parameterized value is : " + myName);
}
}
testng.xml
file, we need to use ‘parameters
’ tag. Look at the below code for example :<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name=”CustomSuite">
<test name=”CustomTest”>
<parameter name="myName" value=”John"/>
<classes>
<class name="ParameterizedTest1" />
</classes>
</test>
</suite>
# | JXL jar | POI jar |
---|---|---|
1 | JXL supports “.xls” format i.e. binary based format. JXL doesn’t support Excel 2007 and “.xlsx” format i.e. XML based format | POI jar supports all of these formats |
2 | JXL API was last updated in the year 2009 | POI is regularly updated and released |
3 | The JXL documentation is not as comprehensive as that of POI | POI has a well prepared and highly comprehensive documentation |
4 | JXL API doesn’t support rich text formatting | POI API supports rich text formatting |
5 | JXL API is faster than POI API | POI API is slower than JXL API |
<a>
(anchor) tag, collect all of the links on a web page.package SeleniumPackage;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
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.chrome.ChromeOptions;
public class BrokenLinks {
public static void main(String[] args) {
String pageURL = "http://www.freetimelearnig.com";
String url = "";
HttpURLConnection huc = null;
int responseCode = 200;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\selenium\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
WebDriver driver = new ChromeDriver(options);//Creating an instance of the WebDriver class
driver.manage().window().maximize();
driver.get(pageURL);
List<WebElement> links = driver.findElements(By.tagName("a")); // getting hold of all the elements having the anchor tag
Iterator<WebElement> it = links.iterator();
// Iterating over the obtained list of elements and checking them one by one
while(it.hasNext()){
url = it.next().getAttribute("href");
System.out.println(url);
if(url == null || url.isEmpty()){
System.out.println("The linked element has invalid href url.");
continue;
}
if(!url.startsWith(pageURL)){
System.out.println("URL belongs to another domain, skipping it.");
continue;
}
try {
huc = (HttpURLConnection)(new URL(url).openConnection());
huc.setRequestMethod("HEAD");
huc.connect(); // connecting to the url
responseCode = huc.getResponseCode(); // reading the response code on firing the url
if(responseCode >= 400){
System.out.println(url+" is a broken link");
}
else{
System.out.println(url+" is a valid link");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
driver.quit();
}
}​
public class ChromeBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.chrome.driver property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = newChromeDriver();
//Using get() method to open a webpage
driver.get("http://freetimelearning.com");
//Closing the browser
driver.quit();
}
}