Created
December 23, 2022 19:52
-
-
Save sofiabrun/21f5df5040da7184ea4599ceb87de3e4 to your computer and use it in GitHub Desktop.
ChatGPT output Java + Selenium + JUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
public class AddToCartTest { | |
private WebDriver driver; | |
@Before | |
public void setUp() { | |
// Set up the Chrome driver | |
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); | |
driver = new ChromeDriver(); | |
} | |
@Test | |
public void testAddToCart() { | |
// Open the OpenCart website | |
String url = "http://opencart.abstracta.us/"; | |
driver.get(url); | |
// Select a product to add to the cart | |
WebElement addToCartButton = driver.findElement(By.cssSelector("a.btn.btn-primary")); | |
String productLink = addToCartButton.getAttribute("href"); | |
// Add the product to the cart | |
addToCartButton.click(); | |
// Check the contents of the cart | |
driver.get(url + "index.php?route=checkout/cart"); | |
WebElement cartTable = driver.findElement(By.cssSelector("table.table.table-bordered")); | |
boolean productAdded = false; | |
for (WebElement row : cartTable.findElements(By.cssSelector("tr"))) { | |
WebElement productColumn = row.findElement(By.cssSelector("td.text-left a")); | |
if (productColumn.getAttribute("href").equals(productLink)) { | |
productAdded = true; | |
break; | |
} | |
} | |
// Assert that the product was successfully added to the cart | |
Assert.assertTrue(productAdded); | |
} | |
@After | |
public void tearDown() { | |
// Close the browser | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment