Write a Selenium script to navigate to a URL and verify that a specific element is present on the page.
Write a Selenium script to perform a login operation on a web application and verify that the login is successful.
Write a Selenium script to find all links on a page and print their URLs to the console.
Write a Selenium script to click on a button and handle the resulting alert.
Write a Selenium script to select a value from a dropdown list and verify that the correct value has been selected.
Write a Selenium script to perform a search operation on a web page and verify that the search results are displayed.
Write a Selenium script to handle file uploads on a web page.
Write a Selenium script to capture a screenshot of a web page.
Write a Selenium script to simulate a right-click operation on a web element.
Write a Selenium script to perform a drag and drop operation on a web page.
Write a Selenium script to navigate to a URL and verify that a specific element is present on the page.
Here's an example Selenium script that navigates to a URL and verifies that a specific element is present on the page using the ExpectedConditions class in Selenium:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class VerifyElementPresence {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL
driver.get("https://www.example.com");
// Wait for the element to be present on the page
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("element-id")));
// Verify that the element is present on the page
if (element.isDisplayed()) {
System.out.println("Element is present on the page.");
} else {
System.out.println("Element is not present on the page.");
}
// Close the browser
driver.quit();
}
}
This script uses the WebDriverWait class to wait for the element to be present on the page using the `ExpectedConditions.presenceOf
2) Write a Selenium script to perform a login operation on a web application and verify that the login is successful.
Here's an example Selenium script that performs a login operation on a web application and verifies that the login is successful:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL of the login page
driver.get("https://www.example.com/login");
// Enter the username and password
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
username.sendKeys("example_user");
password.sendKeys("example_password");
// Click on the login button
WebElement loginButton = driver.findElement(By.id("loginButton"));
loginButton.click();
// Wait for the page to load and verify that the login was successful
String expectedUrl = "https://www.example.com/dashboard";
String actualUrl = driver.getCurrentUrl();
if (actualUrl.equals(expectedUrl)) {
System.out.println("Login successful");
} else {
System.out.println("Login failed");
}
// Close the browser
driver.quit();
}
}
This script navigates to the login page of a web application and enters the username and password in the respective input fields using the sendKeys() method. It then clicks on the login button using the click() method. After the page loads, the script verifies that the login was successful by comparing the current URL of the page to the expected URL using the getCurrentUrl() method. If the URLs match, the script prints a message to the console indicating that the login was successful. If the URLs don't match, the script prints a message indicating that the login failed. Finally, the script closes the browser using the driver.quit() method.
3) Write a Selenium script to find all links on a page and print their URLs to the console.
Here's an example Selenium script that finds all links on a page and prints their URLs to the console
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LinkTest {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL of the page
driver.get("https://www.example.com");
// Find all links on the page and print their URLs to the console
List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
System.out.println(link.getAttribute("href"));
}
// Close the browser
driver.quit();
}
}
This script navigates to the page whose links you want to find using the get() method, and then uses the findElements() method to find all the links on the page by their tag name, which is "a". It then uses a for loop to iterate through the list of links, and for each link it prints its URL to the console using the getAttribute() method with the argument "href". Finally, the script closes the browser using the driver.quit() method.
4) Write a Selenium script to click on a button and handle the resulting alert.
here's an example Selenium script that clicks on a button and handles the resulting alert:
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 AlertTest {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL of the page
driver.get("https://www.example.com");
// Find the button and click it
WebElement button = driver.findElement(By.id("myButton"));
button.click();
// Wait for the alert to appear and switch to it
Alert alert = driver.switchTo().alert();
// Get the text of the alert and print it to the console
String alertText = alert.getText();
System.out.println(alertText);
// Click the OK button on the alert
alert.accept();
// Close the browser
driver.quit();
}
}
This script first navigates to the page where the button you want to click is located using the get() method. It then finds the button element using the findElement() method with a locator such as an ID or a CSS selector, and clicks it using the click() method.
After clicking the button, the script waits for an alert to appear by switching to it using the switchTo().alert() method. It then gets the text of the alert using the getText() method and prints it to the console. Finally, it clicks the OK button on the alert using the accept() method, and closes the browser using the driver.quit() method.
5) Write a Selenium script to select a value from a dropdown list and verify that the correct value has been selected.here's an example Selenium script that selects a value from a dropdown list and verifies that the correct value has been selected:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropdownTest {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL of the page
driver.get("https://www.example.com");
// Find the dropdown element and create a Select object
WebElement dropdown = driver.findElement(By.id("myDropdown"));
Select select = new Select(dropdown);
// Select a value from the dropdown by visible text
select.selectByVisibleText("Option 2");
// Verify that the correct value has been selected
String selectedOption = select.getFirstSelectedOption().getText();
if (selectedOption.equals("Option 2")) {
System.out.println("The correct value has been selected.");
} else {
System.out.println("The wrong value has been selected.");
}
// Close the browser
driver.quit();
}
}
This script first navigates to the page where the dropdown list you want to select a value from is located using the get() method. It then finds the dropdown element using the findElement() method with a locator such as an ID or a CSS selector, and creates a Select object from it.
After creating the Select object, the script selects a value from the dropdown using one of the selectBy...() methods, such as selectByVisibleText(). It then verifies that the correct value has been selected by getting the text of the first selected option using the getFirstSelectedOption() method and comparing it to the expected value using an if statement.
Finally, the script closes the browser using the driver.quit() method.
6) Write a Selenium script to perform a search operation on a web page and verify that the search results are displayed.here's an example Selenium script that performs a search operation on a web page and verifies that the search results are displayed:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SearchTest {
public static void main(String[] args) {
// Set the system property to use Chrome driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
// Navigate to the URL of the page
driver.get("https://www.example.com");
// Find the search box element and enter a search query
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Selenium");
// Submit the search query
searchBox.submit();
// Verify that the search results are displayed
WebElement results = driver.findElement(By.id("search-results"));
if (results.isDisplayed()) {
System.out.println("Search results are displayed.");
} else {
System.out.println("Search results are not displayed.");
}
// Close the browser
driver.quit();
}
}
This script first navigates to the URL of the web page using the get() method. It then finds the search box element using the findElement() method with a locator such as a name or an ID, and enters a search query using the sendKeys() method.
After entering the search query, the script submits the search query using the submit() method. It then verifies that the search results are displayed by finding an element that contains the search results using the findElement() method, and checking whether it is displayed using the isDisplayed() method.
Finally, the script closes the browser using the driver.quit() method.
7) Write a Selenium script to handle file uploads on a web page.here's an example Java-based Selenium script to handle file uploads on a web page:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FileUploadExample {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Launch the web browser and open the website
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Find the file input element and upload the file
WebElement fileInput = driver.findElement(By.id("file-upload"));
fileInput.sendKeys("/path/to/file");
// Click the submit button to upload the file
WebElement submitButton = driver.findElement(By.id("submit"));
submitButton.click();
// Wait for the file to upload and do something with the result
// Close the browser
driver.quit();
}
}
This code finds the file input element on the web page by its ID, sends the file path to it, and then clicks the submit button to upload the file. Note that the path to the file should be an absolute path. After the file is uploaded, you can add code to wait for the upload to finish and process the result as necessary.
8) Write a Selenium script to capture a screenshot of a web page.
here's an example Java-based Selenium script to capture a screenshot of a web page:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class ScreenshotExample {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Launch the web browser and open the website
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Take a screenshot of the webpage and save it to a file
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotFile, new File("/path/to/screenshot.png"));
} catch (Exception e) {
e.printStackTrace();
}
// Close the browser
driver.quit();
}
}
This code launches a Chrome browser, opens a website, takes a screenshot of the webpage, and saves it to a file. Note that you need to import the TakesScreenshot and OutputType classes to capture a screenshot and FileUtils class from apache.commons.io to copy the file. Once the screenshot is saved, you can do whatever you want with it.
9) Write a Selenium script to simulate a right-click operation on a web element.
here's an example Java-based Selenium script to simulate a right-click operation on a web element:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickExample {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Launch the web browser and open the website
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Find the element to right-click on
WebElement element = driver.findElement(By.id("example-id"));
// Create an Actions object to simulate the right-click
Actions actions = new Actions(driver);
actions.contextClick(element).perform();
// Close the browser
driver.quit();
}
}
This code launches a Chrome browser, opens a website, finds an element to right-click on by its ID, and simulates a right-click on it using the Actions class. Once the right-click is performed, you can add code to handle the resulting context menu as necessary. Note that you need to import the Actions class to simulate the right-click.
10) Write a Selenium script to perform a drag and drop operation on a web page.
here's an example Java-based Selenium script to perform a drag and drop operation on a web page:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DragAndDropExample {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// Launch the web browser and open the website
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Find the source and target elements to drag and drop
WebElement sourceElement = driver.findElement(By.id("source-id"));
WebElement targetElement = driver.findElement(By.id("target-id"));
// Create an Actions object to perform the drag and drop
Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElement, targetElement).perform();
// Close the browser
driver.quit();
}
}
This code launches a Chrome browser, opens a website, finds the source and target elements to drag and drop by their IDs, and performs the drag and drop using the Actions class. Once the drag and drop is performed, you can add code to handle the resulting changes to the web page as necessary. Note that you need to import the Actions class to perform the drag and drop.
No comments:
Post a Comment