Issue
In my automation test case using Java and Selenium I came across a situation where I need to perform a verification in which I need to do an image search in Google. I am struggling for some hours to find the locator to upload image from my hard disk. Is it possible to upload image from local hard disk? If yes how can I find the locator and how can I upload image?
Has anyone came across similar situation? Any code snippet will be of great help.
I have tried using Java Selenium with xpath for upload image but without success.
Solution
Performing an image search on Google using Selenium involves a multi-step process, including uploading an image. Google's image search doesn't directly support uploading images programmatically through Selenium. However, you can interact with the user interface to achieve this.
Here's a basic Java code:
package Google;
Import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class ImageUpload
{
public static void main(String[] args) {
WebDriver wDriver = new ChromeDriver();
try {
new SeleniumProgram(wDriver).run();
} catch (Exception e) {
}
wDriver.quit();
}
private static class SeleniumProgram {
private final WebDriver wDriver;
public SeleniumProgram(WebDriver mWDriver) {
wDriver = mWDriver;
}
public void run() throws Exception {
wDriver.get("https://google.com");
Thread.sleep(3000);
click(findElement(By.xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[3]/div[3]"), 0));
Thread.sleep(3000);
WebElement uploadOption = wDriver.findElement(By.cssSelector("input[type='file']"));
click(uploadOption);
Thread.sleep(3000);
setText(uploadOption, "C:\\Users\\HP\\Downloads\\Sun1.jpg");
Thread.sleep(3000);
}
private WebElement findElement(By by, int timeoutInSeconds) {
if (timeoutInSeconds > 0) {
return new WebDriverWait(wDriver, Duration.ofSeconds(timeoutInSeconds))
.until(driver -> driver.findElement(by));
}
return wDriver.findElement(by);
}
private void click(WebElement element) {
((JavascriptExecutor) wDriver).executeScript("arguments[0].click();", element);
}
private void setText(WebElement element, String text) {
element.sendKeys(text);
}
}
}
Note:
- You need to replace
"path/to/your/image.jpg"
with the actual path of the image file you want to upload.
This code opens Google Chrome , clicks on the camera icon, chooses the 'Upload an image' option, and uploads the specified image. After uploading the image, you can perform additional verifications or actions based on your test case. Make sure to adjust the code as needed for your specific requirements and test environment.
If you wish to perform a hands-on test, I have created a test case using Test Assist, a no-code Selenium editor. I invite you to review and execute the test case by clicking on the following link:
https://app.testassist.dev/program/f53d388a-9468-43ee-99fd-6291e905e701
Answered By - Eira
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.