Testing Colleges

Your ultimate guide.

Learn More

Latest Blog Posts

File Upload Demo Page



Waits Demo Page

Delayed Actions

Drag and Drop Demo Page

Drag and Drop Demo
Element 1
Element 2
Element 3






















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. }




****

String Functions in Python

String Functions in Python:
    Python has a variety of built-in string methods. The number of string methods in Python can vary depending on the version of Python you are using, but here are some commonly used string methods in Python:
  1. str.upper(): Converts a string to uppercase.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.upper())
    • >>> output: TESTING COLLEGES
  2. str.lower(): Converts a string to lowercase.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.lower())
    • >>> output: testing colleges
  3. str.capitalize(): Capitalizes the first letter of the string.
    • Example:
    • >>> text = "testing Colleges blogger"
    • >>> print(text.capitalize())
    • output: Testing colleges blogger
  4. str.title(): Capitalizes the first character of each word in the string.
    • Example:
    • text = "testing Colleges blogger"
    • >>> print(text.title())
    • output: Testing Colleges Blogger
  5. str.strip(): Removes leading and trailing whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.strip())
    • >>> output: "Testing Colleges"
  6. str.lstrip(): Removes leading whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.lstrip())
    • >>> output: "Testing Colleges   "
  7. str.rstrip(): Removes trailing whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.rstrip())
    • >>> output: "   Testing Colleges"
  8. str.replace(old, new): Replaces occurrences of the "old" substring with the "new" substring.
    • Example:
    • >>> text = "I love Java"
    • >>> new_text = text.replace("Java", "Python")
    • >>> print(new_text)
    • output: "I love Python"
  9. str.split(separator): Splits a string into a list of substrings based on the specified separator.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.split("e"))
    • output: ['T', 'sting Coll', 'g', 's']
  10. str.join(iterable): Joins the elements of an iterable (e.g., a list) into a single string using the specified separator.
    • Example:
    • >>> l = ['apple', 'banana', 'mango']
    • >>> text = ', '.join(l)
    • >>> print(text)
    • output: "apple, banana, mango"
  11. str.startswith(prefix): Checks if the string starts with the specified prefix.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> s = text.startswith("Testing")
    • >>> print(s)
    • output: True
  12. str.endswith(suffix): Checks if the string ends with the specified suffix.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> s = text.endswith("Colleges")
    • >>> print(s)
    • output: True
  13. str.find(substring): Searches for the first occurrence of a substring and returns its index.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> n = text.find("Colleges")
    • >>> print(n)
    • output: 8
  14. str.rfind(substring): Searches for the last occurrence of a substring and returns its index.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.rfind("e"))
    • output: 14
  15. str.index(substring): Similar to find(), but raises an exception if the substring is not found.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.find("a"))
    • >>> output: -1
    • >>> print(text.index("a"))
    • >>> output: ValueError: substring not found
  16. str.count(substring): Counts the number of non-overlapping occurrences of a substring.
    • Example:
    • >>> text = "Peter Piper picked a peck of pickled peppers."
    • >>> n = text.count("peck")
    • >>> print(n)
    • output: 1
  17. str.isalpha(): Checks if all characters in the string are alphabetic.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> text2 = "TestingColleges"
    • >>> print(text.isalpha())
    • output: False
    • >>> print(text2.isalpha())
    • output: True
  18. str.isnumeric(): Checks if all characters in the string are numeric.
    • Example:
    • >>> text = "12345"
    • >>> print(text.isnumeric)
    • output: True
  19. str.isalnum(): Checks if all characters in the string are alphanumeric.
    • Example:
    • >>> text = "1a2345"
    • >>> print(text.isalnum())
    • output: True
  20. str.isdigit(): Checks if all characters in the string are digits.
    • Example:
    • >>> print("1234".isdigit())   # True
    • >>> print("123.456".isdigit())   # False
    • >>> print("1,234,567".isdigit())   # False
    • >>> print("".isdigit())   # False
    • -----------------------------------
    • >>> print("1234".isnumeric())   # True
    • >>> print("123.456".isnumeric())   # True
    • >>> print("1,234,567".isnumeric())   # True
    • >>> print("".isnumeric())   # False
  21. str.islower(): Checks if all characters in the string are lowercase letters.
    • Example:
    • >>> text = "testing colleges"
    • >>> print(text.islower())
    • output: True
  22. str.isupper(): Checks if all characters in the string are uppercase letters.
    • Example:
    • >>> text = "TESTING COLLEGES"
    • >>> print(text.isupper())
    • output: True
These are just a few of the many string methods available in Python. You can explore more string methods and their usage in the Python documentation:

Contact Us