Saturday, September 19, 2020

Selenium + Java Programs and explaination

Steps for how to create the Java Project In Selenium


  1. Add Selenium Jars 
    • Download the Jar files related to  OS ( from selenium.org Site)
                       
    • Once the download is completed, please follow the below steps to add the jars to the out project.
      • Create the new folder under your project named as “lib”
      • Then create the subfolder under the “lib” and name it as “Selenium Jars” 
      • Now copy the downloaded jar file to “selenium Jar” Folder
            
    • Once the copy is done, Do the right click on the Jar file and select the “Build Path” and the select the “Add to Build Path”  
    • Once you clicked on the “Add to build path” u can able to see the jar file added to our project under the referenced Libraries.        
                                            
                    
        2.    Write a program to launch browser and navigate to a specified link
    • Now we need to write a sample program to open the browser and go to the mentioned link in the program.    
    • For that we need to create the New Package inside the Package “src”, and name it as “test”(U can name whatever u want).
    • In “test” Package now we need to create the new class name it as “SeleniumTest”.
    • Now we need to write the program in the Class,
      • We need to create the object for the web Driver 


    • In the above image, u can see some errors in the code, that is because we have not imported the required packages to our package.
    • For importing the package we need to move the mouse over the word and select the appropriate package








    • For opening the browser we use the driver.get (" ")


    • Once we run the above program we will get the error as below,


    • For that, we need to add the “Gecko” Driver to our project
      • https://github.com/mozilla/geckodriver/releases


      • Download the driver which is suitable to OS
    • Once the “Gecko” Driver is downloaded, copy this driver to our project like below


    • Once it is copied in our project we need to add the Gecko Driver to project using below method


    • System.setProperty("webdriver.gecko.driver", "C:\\Java_Manideep\\SeleniumTest2\\lib\\GeckoDriver\\geckodriver.exe");
    • You can get the file path for the gecko driver as below
      • Right-click on the Gecko Driver


    • Go to properties and you can see the path




    • If we moved the project to some other laptop or folder, the file path may get change and it leads to the wrong path 
    • To avoid this issue we have to store the project location in one variable and use this variable while passing the path.


    • The “user.dir”, contains the location of the Project where it is pasted or moved.
    • We can pass the above-created variable like below.






  1.  Download the Chrome Diver from below link
    • https://www.seleniumhq.org/download/

    •           
    • Download the chrome driver which is suitable to OS
    • Once you downloaded the driver just copy it and paste it in our project

                            





  1. è In this, we will separate code into modules

·        we are doing the three things in this program

o   setting the browser

o   Setting the browser Configuration

o   Run test

·        For these, we need to create the three separate methods

o   SetBrowser()

o   SetBrowserConfig()

o   Runtest()

  1. Sample program
    1. package Test;

       

      import org.openqa.selenium.WebDriver;

      import org.openqa.selenium.chrome.ChromeDriver;

      import org.openqa.selenium.firefox.FirefoxDriver;

       

      public class SeleniumTest {

       

             static String Browser;

             static WebDriver Driver;

       

             public static void main(String[] args) {

       

                   SetBrowser();

                   SetBrowserConfig();

                   RunTest();

       

             }

       

             public static void SetBrowser() {

       

                   Browser="FireFox"; // Chrome

       

             }

       

             public static void SetBrowserConfig() {

       

                   String ProjectLocation=System.getProperty("user.dir");

       

                   if(Browser.contains("FireFox"))

                   {

                          System.setProperty("webdriver.gecko.driver", ProjectLocation+"\\lib\\GeckoDriver\\geckodriver.exe");

                          Driver=new FirefoxDriver();

                   }

                   else

                   {

                          System.setProperty("webdriver.chrome.driver", ProjectLocation+"\\lib\\ChromeDriver\\chromedriver.exe");

                          Driver=new ChromeDriver();

                   }

             }

             public static void RunTest() {

       

                   Driver.get("Https://google.com");

                   Driver.quit();

       

             }

       

      }

Selenium Notes

Locators:->

 

Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.

 

identifier =id 

id 

name 

dom = javascriptExpression 

xpath = xpathExpression 

link = textPattern

css = cssSelectorSyntax

 

Most Used Commands

 

 

driver.get("URL") à To navigate to an application.

driver.navigate().to("URL") à Navigate to the URL.

 

element.sendKeys("inputtext") à Enter some text into an input box.

element.clear() à Clear the contents from the input box.

 

select.deselectAll() à Deselect all OPTIONs from the first SELECT on the page

select.selectByVisibleText("some text") à Select the OPTION with the input specified by the user.

 

driver.switchTo().window("windowName") à Move the focus from one window to another.

 

driver.switchTo().frame("frameName") à Swing from frame to frame.

 

driver.switchTo().alert() à Helps in handling alerts.

 

driver.navigate().forward() à To navigate forward.

driver.navigate().back() à To navigate back.

 

driver.close() à Closes the current browser associated with the driver.

 

driver.quit() à Quits the driver and closes all the associated window of that driver.

 driver.refresh() à Refreshes the current page.    


Key Board actions

 

Using the ACTIONS class we can pass the keyboard keys to the program.

è import org.openqa.selenium.interactions.Actions;

è Actions Act=new actions(<driver object name>);

TAB

ENTERà Act.sendKeys(Keys.RETURN).perform();

CTRL+ALT+DELETE

  

MOUSE ACTIONS

MOVE MOUSE

RIGHT-CLICK

DOUBLE CLICK

DRAG AND DROP