Skip to content

Instantly share code, notes, and snippets.

@sofiabrun
Created December 23, 2022 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sofiabrun/6d5aa5749accc4f87f18ce5f09ff379e to your computer and use it in GitHub Desktop.
Save sofiabrun/6d5aa5749accc4f87f18ce5f09ff379e to your computer and use it in GitHub Desktop.
ChatGPT output Java + Selenium + JUnit - Page Object Model
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
ProductPage productPage = new ProductPage(driver);
String productLink = productPage.getProductLink();
// Add the product to the cart
productPage.addToCart();
// Check the contents of the cart
CartPage cartPage = new CartPage(driver);
boolean productAdded = cartPage.isProductInCart(productLink);
// Assert that the product was successfully added to the cart
Assert.assertTrue(productAdded);
}
@After
public void tearDown() {
// Close the browser
driver.quit();
}
// Page object for the product page
class ProductPage {
private WebDriver driver;
public ProductPage(WebDriver driver) {
this.driver = driver;
}
public String getProductLink() {
WebElement addToCartButton = driver.findElement(By.cssSelector("a.btn.btn-primary"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment