moreLocators
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 9s
Gitea Actions Demo / mvnSelBuild (push) Failing after 49s

This commit is contained in:
2024-09-18 03:06:04 -04:00
parent 40af12f5dc
commit fcc3e156b4
3 changed files with 104 additions and 41 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

@@ -0,0 +1,50 @@
package zacksolutions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.lang.reflect.Array;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
public class AmazonQuestion {
public static WebDriver driver;
public static String[] items = { "Brocolli", "Beetroot", "Beans", "Carrot", "Potato" };
public static void main(String[] args) throws InterruptedException {
System.setProperty("web-driver.gecko.driver", "/home/ilyes/LearnSelenium/DemoProject/drivers/geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String url = "https://rahulshettyacademy.com/seleniumPractise/";
driver.get(url);
Thread.sleep(3000);
try {
getItems();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
driver.close();
}
}
public static void getItems() throws InterruptedException {
List<WebElement> itemsName = driver.findElements(By.cssSelector("h4[class$='product-name']"));
List<String> listOfItems = Arrays.asList(items);
int lastItem = 0;
for (int i = 0; i < itemsName.size(); i++) {
String item = itemsName.get(i).getText().split(" - ")[0].trim();
if (listOfItems.contains(item)) {
driver.findElements(By.xpath("//div[@class='product-action']/button")).get(i).click();
lastItem++;
if (lastItem == listOfItems.size()) {
break;
}
}
}
}
}
+17 -4
View File
@@ -1,20 +1,29 @@
package zacksolutions;
import org.apache.maven.surefire.shared.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
public class Locators {
public static void main(String[] args) throws InterruptedException {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException, IOException {
Locators lc = new Locators();
lc.loginPage();
getScreenShot(driver);
}
public void loginPage() throws InterruptedException {
System.setProperty("web-driver.gecko.driver", "/home/ilyes/LearnSelenium/DemoProject/drivers/geckodriver");
WebDriver driver = new FirefoxDriver();
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String url = "https://rahulshettyacademy.com/locatorspractice/";
driver.get(url);
@@ -45,9 +54,13 @@ public class Locators {
Thread.sleep(1000);
String homePage = driver.getTitle();
System.out.println(homePage);
Thread.sleep(2000);
driver.close();
}
public static void getScreenShot(WebDriver driver) throws IOException {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("/home/ilyes/SeleniumPractice/SeleniumIntro/screenshot.png"));
System.out.println("Picture taken... Please check your forlder");
driver.close();
}
}