Java · TestNG
Advertising with Selenium
A 5-step campaign wizard — objective, audience with location autocomplete, budget & bidding, creative with character counters and a live preview — then launch and pause/resume.
App under test
📣 AdPulse
- URL
- /apps/ads
- Login
- not required
- Min budget
- $5/day · $100 lifetime
- Review time
- ~3 seconds
Scenarios to automate
Work top to bottom — each one adds a new technique.
- 1
Step validation
Beginnerwizard-next on an empty step shows step-errors and error-* for that step only.
- 2
Autocomplete chips
IntermediateType in location-input, pick location-option-*, then remove a chip with remove-location-*.
- 3
Dependent fields
Intermediatebid-strategy “Cost cap” reveals cost-cap-amount; unticking run-continuously reveals run-duration.
- 4
Character counters
Intermediateheadline-count tracks 0/40 and input stops at 40 characters (maxlength).
- 5
Live preview
Intermediatepreview-headline and preview-cta mirror the creative fields as you type.
- 6
Edit from review
Advancededit-step-2 jumps back to Audience; values are preserved when returning to Review.
- 7
Async status
AdvancedA launched campaign shows “In review”, then flips to “Active” ~3s later.
- 8
Toggle switches
BeginnerFlip toggle-<id> (role=switch) and assert aria-checked and the status badge.
Sample Selenium suite
A working starting point that finds elements every way Selenium can — by role, label, text, id, name, CSS, XPath and data-testid — so you practice them all. Pick your language, switch to data-testid only to compare, or try your own in the Locator Playground.
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.Map;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
// Locators in this suite: By.id ×6 · By.name ×4 · By.xpath ×1 · By.cssSelector ×1 · data-testid ×14
public class AdPulseCampaignTest {
static final String BASE = "https://www.practiceqaautomation.com";
WebDriver driver;
WebDriverWait wait;
// Small helpers: every action takes a By and waits for its element first (no Thread.sleep).
By tid(String id) { return By.cssSelector("[data-testid='" + id + "']"); }
WebElement ready(By by) { return centre(wait.until(elementToBeClickable(by))); }
void click(By by) { ready(by).click(); }
WebElement nth(String id, int n) { return centre(wait.until(numberOfElementsToBeMoreThan(tid(id), n)).get(n)); }
void clickNth(String id, int n) { nth(id, n).click(); }
// Scroll to the middle of the screen first: Selenium only scrolls an element to the edge,
// where a sticky header or a fixed bottom bar can cover it (ElementClickInterceptedException).
WebElement centre(WebElement e) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center', inline: 'nearest'})", e);
return e;
}
void type(By by, String text) { ready(by).sendKeys(text); }
void select(By by, String value) { new Select(ready(by)).selectByValue(value); }
void check(By by) { WebElement e = ready(by); if (!e.isSelected()) e.click(); }
void expectText(By by, String text) { wait.until(textToBePresentInElementLocated(by, text)); }
void expectVisible(By by) { wait.until(visibilityOfElementLocated(by)); }
void expectNthText(String id, int n, String text) {
wait.until(d -> d.findElements(tid(id)).size() > n && d.findElements(tid(id)).get(n).getText().contains(text));
}
@BeforeMethod
public void setUp() {
ChromeOptions options = new ChromeOptions();
// Turn off Chrome's "Save password?" and "password found in a data breach" pop-ups: after a
// login they take the keyboard and mouse away from the page, so later typing and clicks are lost.
options.setExperimentalOption("prefs", Map.of(
"credentials_enable_service", false,
"profile.password_manager_enabled", false,
"profile.password_manager_leak_detection", false));
driver = new ChromeDriver(options);
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
@AfterMethod(alwaysRun = true)
public void tearDown() { driver.quit(); }
@Test
public void createsAndLaunchesACampaign() {
driver.get(BASE + "/apps/ads");
click(tid("create-campaign"));
wait.until(urlContains("/apps/ads/new"));
click(tid("objective-traffic"));
type(By.id("campaign-name"), "Diwali Sale");
click(tid("wizard-next"));
// Audience
select(By.name("age-min"), "25");
select(By.id("age-max"), "45");
type(By.name("location-input"), "Mum");
click(By.xpath("//button[normalize-space()='Mumbai, India']"));
check(By.id("interest-shopping"));
expectText(tid("audience-reach"), "people");
click(tid("wizard-next"));
// Budget
click(tid("budget-type-daily"));
type(By.name("budget-amount"), "50");
click(tid("wizard-next"));
// Creative with live preview
type(By.id("ad-headline"), "Big Diwali Sale up to 50% off");
type(By.id("ad-description"), "Lights, gifts and more at festive prices.");
type(By.cssSelector("[placeholder='https://']"), "https://example.com/diwali");
select(By.name("ad-cta"), "shop-now");
expectText(tid("preview-headline"), "Big Diwali Sale");
expectText(tid("preview-cta"), "Shop now");
click(tid("wizard-next"));
expectText(tid("review-name"), "Diwali Sale");
check(By.id("accept-ad-policy"));
click(tid("launch-campaign"));
expectVisible(tid("campaign-launched"));
expectText(tid("campaigns-table"), "Diwali Sale");
}
}