Your ultimate guide

Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

File Upload Using Selenium WebDriver

Exploring Different Approaches for File Upload with Selenium WebDriver:

........Incomplete.......
    File uploads are a common functionality on web applications, and automating this process is crucial for efficient testing with Selenium WebDriver. In this blog post, we will explore three different methods to upload files using Selenium WebDriver: 'SendKeys', the 'Robot' class, and 'AutoIT'.

Using SendKeys:
    The most straightforward approach for uploading a file in Selenium WebDriver is to use the 'SendKeys' method. This method is applicable only if the HTML input element has the attribute type="file". This attribute is specific to file upload functionality, and using Selenium's sendKeys method allows for the automatic selection of the desired file for this type of input field.
Syntax:
  1. WebElement fileInput = driver.findElement(By.id("idvlaue"));
  2. fileInput.sendKeys("path/to/your/file.txt");
Pros:
  • Directly interacts with the file input element.
  • Simple and concise code for file uploads.
  • No external dependencies are required.
Cons:
  • Limited to scenarios where the file input element is present.

Using Robot Class:
    The Robot class in Java can be used to simulate keyboard and mouse actions. In the context of the uploads, the Robot class can be used to navigate the file selection dialogue.

Example Program:
  1. import java.awt.AWTException;
  2. import java.awt.Robot;
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.StringSelection;
  5. import java.awt.event.KeyEvent;
  6.  
  7. import org.openqa.selenium.By;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.WebElement;
  10. import org.openqa.selenium.chrome.ChromeDriver;
  11. import org.openqa.selenium.chrome.ChromeOptions;
  12.  
  13. public class Dummy2 {
  14. public static void main(String[] args) throws AWTException {
  15. ChromeOptions co = new ChromeOptions();
  16. co.addArguments("-remote-allow-origins");
  17.  
  18. WebDriver driver = new ChromeDriver();
  19.                 driver.get("https://testingcolleges.blogspot.com/2024/01/file-upload-demo-page.html");
  20.  
  21. WebElement fileUploadBtn = driver.findElement(By.className("file-label-2"));
  22. fileUploadBtn.click();
  23.  
  24. Robot rbt = new Robot();
  25. rbt.delay(2000); // just like thread.sleep
  26.  
  27. //these below 2 lines used to copy the path 
  28. StringSelection ss = new StringSelection("C:\\Users\\Dollar\\Desktop\\Dummy.txt");
  29. // copy the above path in clipboard
  30. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
  31. //Toolkit.getDefaultToolkit().getSystemClipboard().setContents("path", null);
  32.  
  33. //perform Ctrl+V operation to pass the copied path
  34. rbt.keyPress(KeyEvent.VK_CONTROL);
  35. rbt.keyPress(KeyEvent.VK_V);
  36. rbt.keyRelease(KeyEvent.VK_CONTROL);
  37. rbt.keyRelease(KeyEvent.VK_V);
  38.  
  39. //Enter press
  40. rbt.keyPress(KeyEvent.VK_ENTER);
  41. rbt.keyRelease(KeyEvent.VK_ENTER);
  42.  
  43. }
  44. }

Pros:
  • Works across different browsers.
  • Can handle scenarios with hidden file input fields.
Cons:
  • Requires handling keyboard events, which may be error-prone.
  • Platform-dependent and may need adjustments for different operating systems.
Using AutoIT:
    AutoIT is a scripting language designed for automating the Windows GUI and general scripting tasks. It can be integrated with Selenium WebDriver to handle file Uploads.
Pros:
  • Powerful for handling complex file upload scenarios.
  • Works seamlessly across different browsers and operating systems.
Cons:
  • Requires installing AutoIT and writing an external script.
  • Add an external dependency to your automation project.


Conclusion:
    Each method for file upload with Selenium WebDriver has its advantages and limitations. The choice of method depends on the specific requirements of your application and the level of control and compatibility needed. While SendKeys is suitable for simple scenarios, the Robot class and AutoIT offer more flexibility and control, making them valuable tools for handling complex file upload functionalities.




Browser Window Sizes and Positions


maximize(): The maximize() method is used to maximize the browser window. This is often done to ensure that the web page is displayed in its full size on the screen. Here's an example of how you can use the 'maximize' method in selenium with Java:

  1. public class Maximize {
  2.     public static void main(String[] args) {
  3.         WebDriver driver = new ChromeDriver();
  4.         driver.manage().window().maximize();
  5.     }
  6. }

How to Minimize the Browser Window in Selenium?
    Selenium's Java has no built-in method to minimize the browser window. However, Selenium's Python does have a minimize function.




getSize(): The getSize() method is used to get the height and width of a WebElement/BrowserWindow. The getSize() method returns an object of type 'Dimension', and you can use its 'getHeight' and 'getWidth' methods to obtain the height and width, respectively.

Example 1: To get the height and width of a web element.
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.Dimension;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.chrome.ChromeDriver;
  6. public class GetElementSize {
  7.     public static void main(String[] args) {
  8.         WebDriver driver = new ChromeDriver();
  9.         driver.get("https://example.com");
  10.         WebElement element = driver.findElement(By.id("value"));
  11.         Dimension size = element.getSize();
  12.         System.out.println("Height: " + size.getHeight());
  13.         System.out.println("Width: " + size.getWidth());
  14.     }
  15. }
Example 2: To get the height and width of a Browser Window.
  1. import org.openqa.selenium.Dimension;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. public class GetBrowserWindowSize {
  5.     public static void main(String[] args) {
  6.         WebDriver driver = new ChromeDriver();
  7.         driver.get("https://example.com");
  8.         Dimension windowSize = driver.manage().window().getSize();
  9.         System.out.println("Window Height: " + windowSize.getHeight());
  10.         System.out.println("Window Width: " + windowSize.getWidth());
  11.     }
  12. }
Note: In the above example instead of creating the Dimension class object we can also directly give the methods:
driver.manage().window().getSize().getHeight();
driver.manage().window().getSize().getWidth();




Resize the Browser window:
    To resize the Browser window. 'manage().window().setSize()' is a predefined method of the Selenium 'WebDriver' class.
Syntax 1:
  1. Dimension newSize = new Dimension(800, 600); //(width, height)
  2. driver.manage().window().setSize(newSize);

Syntax 2:
  1. driver.manage().window.setSize(new Dimension(800, 600));

Example:
  1. import org.openqa.selenium.Dimension;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. public class ResizeWindow {
  5.     public static void main(String[] args) {
  6.         System.setProperty("webdriver.chrome.driver", "path/chromedriver.exe");
  7.         WebDriver driver = new ChromeDriver();
  8.         driver.get("http://example.com/");
  9.         Dimension newSize = new Dimension(800, 600);
  10.         driver.manage().window().setSize(newSize);
  11.     }
  12. }
Note: In Eclipse, you may have recommend two import statements for the 'Dimension' class: 'import java.awt.Dimension;' and 'import org.openqa.selenium.Dimension;'. You should only select 'import org.openqa.selenium.Dimension;'. 




getPosition():
    getPosition() method gets the position of the current window, relative to the upper left corner of the screen. The method takes no arguments and returns a Point object, which contains the X and Y coordinates of the window.

Syntax:(With Point object)
  1. // Get the position of the current window
  2. Point p = driver.manage().window().getPosition();
  3. // Print the X and Y coordinates of the window
  4. System.out.println("X Coordinate: " + p.getX());
  5. System.out.println("Y Coordinate: " + p.getY());
Syntax: (We can also print the X and Y Coordinates without creating a Point object)
  1. System.out.println("X Coordinate: "driver.manage().window().getPosition().getX());
  2. System.out.println("Y Coordinate: " + driver.manage().window().getPosition().getY());

Example:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class WindowPosition {
  4.     public static void main(String[] args) {
  5.         System.setProperty("webdriver.chrome.driver", "path/chromedriver.exe");
  6.         WebDriver driver = new ChromeDriver();
  7.         driver.get("http://testingcolleges.blogspot.com/");
  8.         System.out.println("X Coordinate: " + driver.manage().window().getPosition().getX());
  9.         System.out.println("Y Coordinate: " + driver.manage().window().getPosition().getY());
  10.     }
  11. }
Output:
X Coordinate: 10
Y Coordinate: 10




setPosition():
    The setPosition() method in Selenium Webdriver sets the position of the current window relative to the top left corner of the screen. The Point class instance specifies the target position of the window.

Syntax 1:
  1. Point p = new Point(250, 250);
  2. driver.manage().window().setPosition(p);
Syntax 2:
  1. driver.manage().window().setPosition(new Point(250, 250));

Example:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.openqa.selenium.Point;
  4. public class Position {
  5.     public static void main(String[] args) {
  6.         System.setProperty("webdriver.chrome.driver", "path/chromedriver.exe");
  7.         WebDriver driver = new ChromeDriver();
  8.         driver.get("http://testingcolleges.blogspot.com/");
  9.         Point p = new Point(250,250);
  10.         driver.manage().window().setPosition(p);
  11.     }
  12. }




****

Handling Alerts with Selenium


Handling Alerts:
    In Selenium, alerts are popup dialog boxes that appear on a web page to provide information, take user input or request confirmation of specific actions. Selenium provides methods to interact with these alerts, allowing you to automate actions such as accepting, dismissing, or entering text into them.
Here's how you can work with alerts in Selenium:

switchTo().alert():
    To interact with an alert, you first need to switch the WebDirver's focus to it using the 'switchTo().alert()' method, and then we need to perform the action.
  1. Accept an Alert: Click OK an alert and close it
    • driver.switchTo().alert().accept();
  2. Dismiss an Alert: Click Cancel or the equivalent of an alert and close it.
    • driver.switchTo().alert().dismiss();
  3. Enter Text into a Prompt Alert: If the alert is prompt that requires you to enter text, you can use the 'sendKeys()' method to provide input.
    • driver.switchTo().alert().sendKeys("Your Input text");
  4. Get Text from an Alert: To retrieve the text displayed in an alert.
    • driver.switchTo().alert().getText();

Example Program:
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;

  5. public class Alert2 {
  6.   public static void main(String[] args) throws InterruptedException {
  7.     WebDriver driver = new ChromeDriver();
  8.     driver.get("https://testingcolleges.blogspot.com/2023/08/alerts-demo-pages.html");
  9.     WebElement confirmbox = driver.findElement(By.id("confirmbox"));
  10.     confirmbox.click();
  11.     driver.switchTo().alert().accept();
  12.     confirmbox.click();
  13.     driver.switchTo().alert().dismiss();
  14.     WebElement promptbox = driver.findElement(By.id("promptbox"));
  15.     promptbox.click();
  16.     driver.switchTo().alert().sendKeys("Happy");
  17.     driver.switchTo().alert().accept();
  18.   }
  19. }



 
switchTo().activeElement()
    To automate active element current webpage.
For example, when we open some websites, by default one element will be selected to select such type of active element we will use this method.

Example: driver.switchTo().activeElement().sendKeys(“Hello”);





Web Elements Locators

How to Inspect the web elements of a web page?

  • Every web page has different web elements.
    • To identify the web element, use the developer tool to inspect the element.
    • For that, Right-Click on the Web Page ➡ Select Inspect ➡ Select the Element tab. (or Shortcut Key F12)
  • Every web element contains tags.
  • Those tags contain attributes.
  • Attributes contain values.
  • Example: <span class="DPvwYc GHpiyd" aria-hidden="true">Hello</span>
    Note: Web elements are common for all browsers.



What is findElement?
    It is the method used to locate elements on the webpage.
What is By?
    By is the mechanism used to locate elements within documents with the help of locator value.
What is a Locator?
    It is an address that identifies a web element uniquely within the web page.




Locators:
    There are eight types of locators.

  1. id
  2. name
  3. xpath
  4. cssSelector
  5. className
  6. tagName
  7. linkText
  8. partialLinkText

id Locator:
  • We can identify elements by using the ‘id’ attribute.
    • Syntax: <object>.findElement(By.id("id_value"));
    • Example: driver.findElement(By.id("dollar"));

  •     Example Program:
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. public class LocatorID {
  5.    public static void main(String[] args) {
  6.       WebDriver driver = new ChromeDriver();
  7.       driver.get("https://testingcolleges.blogspot.com/");
  8.       driver.findElement(By.id("dollar")).click();
  9.    }
  10. }



name Locator:
  • Name attribute can also identify an element.
    • Example: driver.findElement(By.name(“value”));



       xpath:

Xpath is an XML path used for navigation through the HTML structure of the page.

Right-click on the tag of the particular field (on - DOM) ➡Select copy ➡ Select copy x path

 
There are two types of XPath
1.    Absolute path:
a.    Full path from top to bottom /end.
b.    It starts with a single slash (/).
c.     Search from the root node
Example: /html/body/div/input/div[2]/ul/li[2]/……..
2.    Relative XPath:
a.    Middle of the path.
b.    It starts with a double slash (//).
c.     Search starts from the middle of the DOM
Example: //*[@id=’value’]

          Example: driver.findElement(By.xpath(“//input[@id=’value’]”))


  • Example program:
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. public class LocatorXpath {
  6.    public static void main(String[] args) {
  7.       WebDriver driver = new ChromeDriver();
  8.       driver.get("https://testingcolleges.blogspot.com/");
  9.       WebElement elm = driver.findElement(By.xpath("//*[@id=\'dollar\']/a/span/b/span"));
  10.       elm.click();
  11.    }
  12. }


cssSelector:

CSS is used to create style rules for web pages and can be used to identify any web element.

Right-Click on a particular element tag à Select Copy à Select Copy Selector


  1. It starts with #.
  2. Xpath does not work in the Internet Explorer browser in that scenario they will use cssSelector.
  3. XPath and cssSelectors are both the same.
  • Example: driver.findElement(By.cssSelector(“#input[@attribute]”))



className:
    A ClassName operator uses a class attribute to identify an object.
        Example: driver.findElement(By.className(‘value’))



tagName:
    In real-time we rarely use it. Here we will assign the values based on the main tag, not an attribute.
  • Example: driver.findElement(By.tagName(“INPUT”));
Note: The tag name can be declared using either capital or lowercase letters. 

  • Example Program: Get the number of links on a web page using Selenium with Java?
  1. import java.util.List;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. public class Links {
  7. public static void main(String[] args) {
  8. WebDriver driver = new ChromeDriver();
  9. driver.get("https://www.google.com");
  10. List<WebElement> l = driver.findElements(By.tagName("a"));
  11. System.out.println(l.size());
  12. driver.quit();
  13. }
  14. }
  15. //Output: 25




linkText:
    Text (visible text) used in hyperlinks can also locate elements.
        Example: driver.findElement(By.linkText(“Gmail”));
  • Example program:
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. public class LocatoeLinkText {
  6.    public static void main(String[] args) {
  7.       WebDriver driver = new ChromeDriver();
  8.       driver.get("https://testingcolleges.blogspot.com/");
  9.       WebElement elm = driver.findElement(By.linkText("C# vs Java"));
  10.       elm.click();
  11.    }
  12. }



partialLinkText:
    Part of the text in the link can also identify an element.
        Example: link text:- click here to download this song
            driver.findElement(By.partialLinkText(“click here”));



- Next Page: Navigation Commands

About Selenium

What is Selenium?
    It is the suite of open-source tools to automate web applications (only web applications) across different operating systems and browsers

Similar software like selenium -> UFT, QTP, Cypress, Tosca, -----

Component of selenium:
1. Selenium IDE 
2. Selenium Web Driver 
3. Selenium Grid 
4. Selenium RC (Remote Control)
5. Selenium IDE (Integrated Development Environment):

Selenium IDE (Integrated Development Environment):
    Is an open-source web automation testing tool. It’s not an executable program, it’s just a plug-in/extension & that supports only Chrome and Firefox web browsers.

    Selenium IDE – records the browser interactions (Action) & playback tests in the browser.


       Even without knowing the programming language, we can automate the web application – In real-time organizations, we never use it – It will record all your actions and save it as a package & we can play that repeatedly.

    We can also export this actions file package to different programming languages like c#, Java, Python, and Ruby (automatically code will be generated.)

For that: - Right click on the action file -> click on Export -> Select language we want to convert -> Click on Export -> Save the file

 
 
Selenium Web Driver:

It is a web automation framework that allows you to execute your automation script against different browsers and operating systems. 
Selenium Web Driver drives a browser natively, as a real user would, either locally or on remote machines. 
It is also known as Selenium 2.0. 
All Selenium Projects released under the Apache 2.0 License
It Supports browsers such as Chrome, Firefox, Edge, Safari, Opera, HtmlUnitDriver (Headless browser)

  • HTML UnitDriver is the most lightweight and fastest implementation headless browser for WebDriver. It is based on HtmlUnit. 
  • It is known as Headless Browser Driver. It is the same as Chrome, IE, or Firefox driver, but it does not have GUI so one cannot see the test execution on screen.


- It supports in maximum all popular Operating systems such as – Windows Operating System, Mac OS, and Linux. 
- It supports programming languages such as – Java, c#, Ruby, Python, Java Script, -Kotlin. 

Advantages:
1. It is an open-source, freeware and portable tool
2. It supports many operating systems.
3. It supports various browsers.
4. It supports various programming languages.
5. It supports parallel test execution.
6. It uses very less CPU and RAM consumption for script execution.
7. It can be integrated with the TestNG testing framework for testing our applications and generating reports.
 8. It can be integrated with Jenkins, Maven, GitHub and other tools for DevOps implementation. 
 
Disadvantages:
1. It only supports web-based applications and does not support windows based applications.
2. No centralized maintenance of objects.
3. It is difficult to test Image based applications.
4. No reliable support like Commercial Tools.
5. Difficult to set up the environment.
6. It needs outside support for report generation activity like dependence on TestNG or Jenkins.

 

Selenium Grid:
- If you want to scale by distributing and running tests on several machines and manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers/OS, then you want to use Selenium Grid.
- The Grid can minimize test runtime—by executing multiple test scripts on any number of remote devices at once. This is called parallel testing.
- The two main components needed for this (other than the test script from client-side/tester) are:
1.  ‘Hub’ (server):
     Accepts access requests from WebDriver clients. Routes JSON test commands to remote drivers on registered ‘nodes’.
2.  ‘Node’ (remote device):
    Contains a native OS, browsers, and remoteWebDriver.

Selenium RC:
In selenium Rc, to run automation Script on the browser, we need a Remote Control Server to Communicate with the web browser.
- This is a very old process, also known as Selenium 1.
- Now we have selenium webDriver, by webDriver, we can interact with a web browser.

History of Selenium: 

*  2004 -Selenium was created by Jason Huggins at ThoughtWorks in Chicago.
*  2005 -Selenium RC (Remote Control).Also known as Selenium1.0
*  2006 -Selenium IDE was created by Shinya Kasatani. Shinya Kasatani wraps the Selenium driver code in an IDE module in the Firefox browser. He donated to the Selenium Project.
*  2007 -Selenium WebDriver was launched at Google
*  2008 -The whole Selenium team decided to merge Selenium WebDriver and Selenium RC in order to form a more powerful tool called Selenium 2.0
*  2008: At Thought Works, Philippe Hanrigoucreates a server which would allow testers to access and run tests on browser instances on any number of remote devices. This becomes known as the Grid
*  2016:Selenium RC gets deprecated and WebDriver becomes standard implementation—Selenium 3.0.
*  2019:WebDriver becomes a W3C standard protocol

CSS Selector

 CSS Selector:
1) id Attribute: ( # )
XPath: //div[@id='fname']
CSS    : div#fname
 
2) class Attribute: ( )
XPath: //div[@class='name']      (or) //*[@class='name']
CSS    : div.name      (or) .name

3) Attribute:
xpath: //tagName[@Attribute='value']      (or) //*[@Attribute='value']
CSS    : tagName[Attibute='value']      (or) [Attribute='value']

4) Sub-String:
Sub-String are of 3 types:
    Matching a prefix(starts-with):  tagName[Attribute ^= 'prefix']
    Matching a suffix(ends-with):  tagName[Attribute $= 'suffix']
    Matching a substring(contains): tagName[Attribute *='partialValue']

5) nth-of-type(index):
XPath: //*[@arial='123'][2]
CSS    : [arial='123'] : nth-of-type(2)

6) Direct Child(following-sibling):(>)
XPath: //div//following-sibling :: a
CSS    : div > a

7) Child/Sub-Childs:(<SPACE>)
XPath: //div//child :: a
CSS    : div a

8)AND:
XPath: //*[@Attribute1='value' and @Attribute2='value']
CSS    : [Attribute1='value'][Attribute2='value']

9) Next-Sibling:( + )
CSS    : div#123 + div