By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.

Blog

This blog page offers a collection of tutorials that demonstrate how to optimize your usage of the TestAssist editor effectively.

Video
December 26, 2023

How To Handle Alerts in Selenium?

Learn More
ReadMore

To handle this task efficiently, it's essential to take a systematic and professional approach. Follow this clear step-by-step guide:

  • Initialize the WebDriver: To kickstart your journey into web automation, the first crucial step is initializing the WebDriver. Follow this foundational Java code snippet:
WebDriver driver = new ChromeDriver();
  • Navigate to the Destination URL: Next, we address the query of how to direct the WebDriver to a specific URL for testing purposes using get method:
driver.get("https://demoqa.com/alerts");
  • XPath Identification: Identify the target element using XPath, employing attributes such as ID, class, or text for precise selection. Create a WebElement for subsequent interaction.
  • For example, let's identify a button on the page:
String searchBoxXPath = "//input[@id='twotabsearchtextbox']";
WebElement button = driver.findElement(By.xpath("//button[@id='confirmButton']"));
  • Performing Actions on the Page: Execute actions on the identified elements, in this case, a button click:
button.click();
  • Handling Alerts: Now, let's tackle the question of how to navigate and dismiss alerts effectively during automation:
Alert alert = driver.switchTo().alert();
alert.dismiss();
  • Closing the Browser: Gracefully close the WebDriver to conclude the scripted interaction:
driver.quit();

By adopting these structured steps, you ensure an optimal and systematic execution of your testing tasks. The script showcases the power of Selenium WebDriver in handling alerts, a common scenario in web applications.Stay tuned for more automation insights to elevate your testing capabilities.

Java Code

Copy Code
package org.example;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertHandler {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("https://demoqa.com/alerts");
        WebElement button = driver.findElement(By.xpath("//button[@id='confirmButton']"));
        button.click();
        Thread.sleep(3000);
        Alert al= driver.switchTo().alert();
        al.dismiss();
        Thread.sleep(3000);
        driver.quit();
            }
        }

Python Code

Copy Code
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://demoqa.com/alerts")
button = driver.find_element(By.XPATH, "//button[@id='confirmButton']")
button.click()
time.sleep(3)
alert = driver.switch_to.alert
alert.dismiss()
time.sleep(3)
driver.quit()

C# Code

CopyCode
using NUnit.Framework.Internal;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace TestProject
{
    [TestClass]
    public class AlertHandlingTests
    {
        [TestMethod]
        public void Handle_Dismiss_Alert()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://demoqa.com/alerts");
            IWebElement button = driver.FindElement(By.XPath("//button[@id='confirmButton']"));
            button.Click();
            Thread.Sleep(3000);
            IAlert al = driver.SwitchTo().Alert();
            al.Dismiss();
            Thread.Sleep(3000);
            driver.Quit();
        }
    }
}
Video
December 25, 2023

How to switch tabs in Selenium?

Learn More
ReadMore

For an optimal and systematic execution of tasks, it is highly recommended to adopt a methodical and strategic approach. Follow this comprehensive step-by-step guide:

  • Initialize the WebDriver: Begin your testing setup by initializing the WebDriver with the following Java code snippet:
WebDriver driver = new ChromeDriver();
  • Navigate to the Destination URL: Direct the WebDriver to your desired URL with the get method:
driver.get("https://www.amazon.in/");
  • XPath Identification: Identify the target element using XPath, utilizing attributes such as ID, class, or text for precise selection. Create a WebElement for subsequent interaction.
  • For instance, let's identify the search bar:
String searchBoxXPath = "//input[@id='twotabsearchtextbox']";
WebElement searchAmazonIn = driver.findElement(By.xpath(searchBoxXPath));
  • Text Input : Enter the desired text for your Amazon search using the sendKeys() method:
searchAmazonIn.sendKeys("Laptop");
  • Amazon Search Button XPath: Identify and click the Amazon search button with the relevant XPath:
driver.findElement(By.xpath("//input[@id='nav-search-submit-button']")).click();
  • Choosing the Preferred Laptop from the Search Results: After obtaining the search results, our next step is to select a  laptop. Utilize XPath to precisely identify and click on the desired laptop:
String desiredLaptopXPath = "(//span[@class='a-size-medium a-color-base a-text-normal'])[1]";
driver.findElement(By.xpath(desiredLaptopXPath)).click();
  • Switching and Closing Tabs:Capture the current window handle, get all window handles into a list, switch to the new tab,  perform actions and efficiently close the newly opened tab. Achieve this with the following code snippet:
List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(windowHandles.get(index)); //specify the index
driver.close();

By following these straightforward steps, you'll seamlessly handle your testing tasks while adhering to the best practices in automation. This approach guarantees reliability, precision, and an overall improvement in the efficiency of your testing efforts.

Java Code

Copy Code
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
public class TabSwitcher {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.amazon.in/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        WebElement searchAmazonIn = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));
        searchAmazonIn.sendKeys("Laptop");
        driver.findElement(By.xpath("//input[@id='nav-search-submit-button']")).click();
        driver.findElement(By.xpath("(//span[@class='a-size-medium a-color-base a-text-normal'])[1]")).click();
        String firstTab = driver.getWindowHandle();
        List windowHandles = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(windowHandles.get(1));
        Thread.sleep(5000);
        driver.close(); // if you need to close the newly opened tab
        driver.quit();
    }
}

Python Code

Copy Code
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.amazon.in/")
driver.implicitly_wait(10)
search_amazon_in = driver.find_element(By.XPATH, "//input[@id='twotabsearchtextbox']")
search_amazon_in.send_keys("Laptop")
driver.find_element(By.XPATH, "//input[@id='nav-search-submit-button']").click()
driver.find_element(By.XPATH, "(//span[@class='a-size-medium a-color-base a-text-normal'])[1]").click()
window_handles = driver.window_handles
driver.switch_to.window(window_handles[1])
time.sleep(5)
driver.close()
driver.quit() 

C# Code

CopyCode
using NUnit.Framework.Internal;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace TestProject1
{
    [TestClass]
    public class AmazonTest
    {
        [TestMethod]
        public void AmazonSearchAndTabSwitching()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.amazon.in/");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            IWebElement searchAmazonIn = driver.FindElement(By.XPath("//input[@id='twotabsearchtextbox']"));
            searchAmazonIn.SendKeys("Laptop");
            IWebElement searchSubmitButton = driver.FindElement(By.XPath("//input[@id='nav-search-submit-button']"));
            searchSubmitButton.Click();
            IWebElement firstSearchResult = driver.FindElement(By.XPath("(//span[@class='a-size-medium a-color-base a-text-normal'])[1]"));
            firstSearchResult.Click();
            var windowHandles = driver.WindowHandles;
            driver.SwitchTo().Window(windowHandles[1]);
            Thread.Sleep(5000);
            driver.Close();
            driver.Quit();
        }
    }
}
Video
December 22, 2023

How to handle dropdowns using Selenium?

Learn More
Read More

For optimal task execution, it is highly advisable to embrace a methodical and strategic approach. Below is a comprehensive step-by-step guide:

  • Initialize the WebDriver: Kickstart your testing environment by initializing the WebDriver using the following Java code snippet:
WebDriver driver = new ChromeDriver();
  • Navigate to the Destination URL: Direct the WebDriver to your desired URL with the get method:
driver.get("https://www.google.com/");
  • XPath Identification: Pinpoint the target element using XPath, leveraging attributes like ID, class, or text for accurate selection. Create a WebElement for further interaction:
WebElement accept =  driver.findElement(By.xpath("//textarea[@id='APjFqb']"));
  • Text Input for Google Search: Send the relevant text for your Google search using the sendKeys() method:
accept.sendKeys("Football");
  • Google Search Button XPath: Identify and click the Google search button with the relevant XPath:
WebElement searchButton = driver.findElement(By.xpath("(//input[@value='Google Search'])[2]"))
  • Activate SafeSearch Dropdown: Identify and click the SafeSearch dropdown with its corresponding XPath:
WebElement safeSearchDropdown = driver.findElement(By.xpath("//div[@class='F75bid']"));
safeSearchDropdown.click();
  • Select 'Off' Option: Specify the XPath for selecting the 'Off' option:
WebElement option = driver.findElement(By.xpath("//div[text()='Off']"));
option.click();

Follow these steps to handle your testing tasks efficiently, aligning with best practices in automation. This approach ensures reliability, precision, and overall improved efficiency in your testing efforts.

Java Code

Copy Code
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class DropdownHandlingTest {
    public static void main(String[] args) throws InterruptedException {
         WebDriver driver = new ChromeDriver();
         driver.get("https://www.google.com/");
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
         WebElement accept =  driver.findElement(By.xpath("//textarea[@id='APjFqb']"));
         accept.sendKeys("Football");
         accept.sendKeys(Keys.ENTER);
         WebElement  SafeSearchDropdown = driver.findElement(By.xpath("//div[@class='F75bid']"));
         SafeSearchDropdown.click();
         WebElement option = driver.findElement(By.xpath("//div[text()='Off']"));
         option.click();
         Thread.sleep(5);
         driver.quit();
    }
}

Python Code

Copy Code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
driver.maximize_window()
driver.implicitly_wait(5)
accept = driver.find_element("xpath", "//textarea[@id='APjFqb']")
accept.send_keys("Football")
accept.send_keys(Keys.ENTER)
safe_search_dropdown = driver.find_element("xpath", "//div[@class='F75bid']")
safe_search_dropdown.click()
option = driver.find_element("xpath", "//div[text()='Off']")
option.click()
time.sleep(5)
driver.quit() 

C# Code

CopyCode
using NUnit.Framework.Internal;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace TestProject1
{
    [TestClass]
    public class DropdownHandlingTest
    {
        [TestMethod]
        public void Handle_Dropdown()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.google.com/");
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            IWebElement accept = driver.FindElement(By.XPath("//textarea[@id='APjFqb']"));
            accept.SendKeys("Football");
            accept.SendKeys(Keys.Enter);
            IWebElement safeSearchDropdown = driver.FindElement(By.XPath("//div[@class='F75bid']"));
            safeSearchDropdown.Click();
            IWebElement option = driver.FindElement(By.XPath("//div[text()='Off']"));
            option.Click();
            Thread.Sleep(5000);
            driver.Quit();
        }
    }
}
Video
December 13, 2023

How To Accept Cookies?

Learn More
Read More

To tackle this task effectively, it's crucial to follow a systematic and professional approach :

  • Begin by initializing the WebDriver to establish the testing environment. Utilize the following code snippet in Java to achieve this.
WebDriver driver = new ChromeDriver();
  • Navigate to the desired URL using the get method.
driver.get("https://www.testassist.dev/");
  • Subsequently, identify the XPath of the targeted cookies button, employing attributes such as ID, class, or text for precise selection.
  • In the example provided, the XPath includes the text condition:
WebElement accept = driver.findElement(By.xpath("//a[text()='Accept']"));
  • Conclude the process by executing a click action on the identified button using the corresponding web element.
accept.click();

This professional approach ensures a robust and efficient handling of cookies within the testing framework, aligning with best practices in test automation.

Java Code

Copy Code
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AcceptCookies {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.testassist.dev/");
        driver.manage().window().maximize();
        WebElement accept = driver.findElement(By.xpath("//a[text()='Accept']"));
        accept.click();
        Thread.sleep(10);
        driver.quit();
    }
}

Python Code

Copy Code
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.testassist.dev/")
driver.maximize_window()
accept = driver.find_element(By.XPATH, "//a[text()='Accept']")
accept.click()
time.sleep(10)
driver.quit() 

C# Code

CopyCode
using NUnit.Framework.Internal;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace TestProject1
{
    [TestClass]
    public class CookieAcceptanceTests
    {
        [TestMethod]
        public void AcceptCookies()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.testassist.dev/");
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            IWebElement accept = driver.FindElement(By.XPath("//a[text()='Accept']"));
            accept.Click();
            Thread.Sleep(5000);
            driver.Quit();                     
        }
    }
}
Video
November 6, 2023

Introduction Video

Learn More
Video
June 7, 2023

Detailed Explainer Video of TestAssist in 13 minutes

Learn More

To tackle this task effectively, it's crucial to follow a systematic and professional approach:

  • Begin by initializing the WebDriver to establish the testing environment. Utilize the following code snippet in Java to achieve this.
WebDriver driver = new ChromeDriver();
  • Navigate to the desired URL using the get method.
driver.get("https://www.testassist.dev/");
WebElement searchAmazonIn = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")
  • Subsequently, identify the XPath of the targeted cookies button, employing attributes such as ID, class, or text for precise selection.
  • In the example provided, the XPath includes the text condition:
WebElement accept = driver.findElement(By.xpath("//a[text()='Accept']"));
driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div[1]/div/span[1]/div[1]/div[3]/div/div/div/div/span/div/div/div/div[2]/div/div/div[1]/h2/a/span")).click();
  • Conclude the process by executing a click action on the identified button using the corresponding web element.
accept.click();

This professional approach ensures a robust and efficient handling of cookies within the testing framework, aligning with best practices in test automation.

Stay in the Loop: Subscribe to Our Test Automation Newsletter!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Free Graphical no-code editor for Generating Selenium automation code in Java, C# & Python

Address

  • info@testassist.dev
  • +4915753223659
  • SwiftSoftware GmbH
  • Ziegeleistraße 36, 1.OG
    74193 Schwaigern, Germany

Copyright © Test Assist | Designed by SwiftSoftware GmbH