QaMantra

How to Open a Link in New Tab in Selenium

In this Example, we will open a link in New tab, So below mentioned step you have to perform to achieve this :

1. Create an XPath or find a unique path of the link.

Ex : As Mentioned in ScreenShot we are going to open a 'CORE JAVA' Link in new Tab So we have taken a Unique Path.


Image 1


2. Get 'href ' Attribute from WebElement, because of Every Link Consist of a Unique URL


String UrlofLink = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]")).getAttribute("href");
or you can Devide Script into Two Parts

WebElement Link = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]"));                                                         String UrlofLink=Link.getAttribute("href");
3. To Open a New Tab you have to use JavaScript Executor and WindowHandler, JavaScriptExecutor has Used for Open a Blank new Tab and Through windowHandler, you will get a count of current Tab based on Count you can switch to parent tab to child Tab and Vice-versa.


public class OpenNewTab{
public static void main(String[] args) throws IOException {
  
 System.setProperty("webdriver.chrome.driver",".\\Exe\\chromedriver.exe"); 
 WebDriver driver=new ChromeDriver();
 String baseUrl = "http://www.QaMantra.com/";
    driver.get(baseUrl);
 
 // Get a Url from Link 
 
 WebElement Link = driver.findElement(By.xpath("//a[contains(text(),'Core Java')]"));                                                         
 String UrlofLink=Link.getAttribute("href");
   
   // To open a New tab 
   
    ((JavascriptExecutor)driver).executeScript("window.open()");
 
 //Get a Count of Current open Window
 
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
 //switches to new tab
    driver.switchTo().window(tabs.get(1));    

    // To open a link in New Tab
 driver.navigate().to(UrlofLink);
    
 // switch back to main screen 
    driver.switchTo().window(tabs.get(0)); 
 
    
 }}




















Locators in Selenium

Locators 

Locators provide a mechanism to access the HTML elements on a web page. Once we locate an element/object on any web page, Then to get or store the Webelement we use findElement() or Findelements()  method to find or strore the located webelement or List of elements then we can write test cases to execute on them.
                                               
                                              OR

Selenium uses what is called locators to find and match the elements of your page that it needs to interact with


                           For example, If we would like click on any button then locators will help me to click on button , We will see that in details in the subsequent sections of this tutorial.


Types of Locators in Selenium 

Selenium webdriver Have 8 locators to find the Web elements on web page , We have mentioned the list of locators as per priority to be used while Coding.


  • ID
  • Name 
  • Link Text
  • Partial Link Text
  • Tag Name 
  • Class Name
  • Css Selector
  • XPATH   - Absolute and Relative 

Locators




Configuration of Selenium WebDriver Project With Java part 2

In Last Article we have seen that what different stuff required for Maven Project and Java Project Configuration  ,In this article we will learn about final configuration of Selenium WebDriver Project


  • Create a Java Project or Maven Project if You want to work on Existing Project then Skip this Step ,otherwise i prefer to create Setup in New Project ,if You do not How To create Simple Java Project Click Here
  • Right click on  Java/Maven Project  >New >Folder > Enter Folder Name Like -'Jar' >Click on Finish
  • For Java Project -Copy Selenium Standalone jar which you have downloaded in First step and Paste Inside Folder 'Jar'  > Right Click on Selenium Standalone jar >Go To Build Path > Click On Add To Build Path ,Maven Project user should skip this Step 
  • For Maven Project Copy Dependency from Previous Article and Paste inside POM.XML Dependencies Tag 
  • Right click on  Java/Maven Project  >New >Folder > Enter Folder Name Like -'Exe' >Click on Finish
  • Copy ChromeDriver.exe File which you have downloaded in First step and Paste Inside Folder 'Exe' for Chrome Browser ,you can use GeckoDriver for Firefox ,IEDriver for Internet Explore and So on 
                          Now your Selenium Configuration beginners phase have been completed now you can Start to Automate to any Web Application .

 Previous Article                                                                                                       Continue Reading ...

Configuration of Selenium WebDriver Project With Java part 1

To install Selenium means to set up a project in a development so you can write a program using Selenium. How you do this depends on your programming language and your development environment and Selenium is Language Independent Tool So You can use any one Programming Language among -  Java , C# , Python , Ruby , PHP , JavaScript , Perl .


Java 
The easiest way to set up a Selenium 2.0 Java project is to use Maven. Maven will download the java bindings (the Selenium 2.0 java client library) and all its dependencies, and will create the project for you, using a maven pom.xml (project configuration) file. Once you’ve done this, you can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse.
  
Maven Project
          
 For Maven Project you Should have Selenium Dependency in POM.xml ,Selenium Dependency you can get from Here .

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.8.1</version>
</dependency>
Copy this Dependency and Paste inside a  <Dependencies>  Paste Selenium Dependency  </Dependencies> Tag 
                  Be sure you specify the most current version. At the time of writing, the version listed above was the most current, however there were frequent releases immediately after the release of Selenium 2.0. Check the Maven download page for the current release and edit the above dependency accordingly.

Now, from a command-line, CD into the project directory and run maven as follows.

mvn clean install
     Java Project  

For those user who is using Simple Java Project they can Download Jar Selenium Latest Jar

Previous Article                                                                                                       Continue Reading ...

Introducing Selenium WebDriver

The primary new feature in Selenium Webdriver 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.



WebDriver Compared to Selenium-RC

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter.

For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

Previous Article                                                                                                        Continue Reading ...


WebDriver and the Selenium-Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly.



There are some reasons though to use the Selenium-Server with Selenium-WebDriver.


  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

Why we assign Browser instance to webdriver interface

WebDriver driver = new FirefoxDriver();

WebDriver driver = new ChromeDriver();

WebDriver driver = new IEDriver();


In the above statement, WebDriver is an interface. An interface contains empty functions that have been defined but not implemented. These functions can be implemented by anyone as long as the method type and signatures can not be violated.

                             Therefore, an interface is also known as contract, because you can use an interface as you like but you cannot change the way it has been defined. And, since it has empty methods you won't actually need to instantiate it and so you cannot instantiate it.

FirefoxDriver/ChromeDriver/IEDriver and so on browser Driver is a class that has been written specifically for the Firefox/Chrome/IE and many browser. It has methods that are implemented and it can be instantiated. It can perform all functions (or methods) on the Firefox browser as defined in the interface WebDriver.


WebDriver API Architecture


So in the above statement, we are actually telling FirefoxDriver/ChromeDriver/IEDriver class that " you can automate the various methods that you want on the Firefox/Chrome/IE browser but you need to stick to the contract defined in WebDriver".

                                   So we declare a reference variable of type WebDriver and then use it to instantiate FirefoxDriver/ChromeDriver/IEDriver, which means that the object (driver) is of type WebDriver but points to the memory allocation to all data and methods in FirefoxDriver/ChromeDriver/IEDriver (and, as mentioned above, the FirefoxDriver/ChromeDriver/IEDriver class already has the implemented version of methods in WebDriver)

Continue Reading