Write Selenium Java. Press Run. Watch it drive the browser.
A TestNG class with a base, a page object and a testng.xml – running against one of our own applications in the pane beside your code. No JDK, no Maven, no driver binary, no setup at all.
Read this first: there is no JVM in a web page
Your Java is not compiled. This page reads your Selenium Java and performs it – By.id, findElement, sendKeys, WebDriverWait, Assert – against the real page in the pane. We are telling you this because the day you run a real suite, you should not be surprised by anything.
- The locators are real.
By.xpathis real XPath, evaluated against the real document. A locator that fails here fails at work. - The waits are real.
WebDriverWaitpolls the page exactly as it would in a real run. - The failures are real. A missing element, a hidden element, a wrong assertion – each one stops the test and names the line.
- Anything it cannot perform stops the run and says so. It never skips a line quietly, because a test that “passed” because four statements were ignored is worse than no test.
- Not supported:
Actionschains,switchTo()for frames, windows and alerts, threads, file I/O, and anything that needs a JVM. - One browser – the pane.
new ChromeDriver()opens that, not a real Chrome process. - Our own applications only. A browser cannot read a page from another site, so an external
driver.get(...)is refused with that reason. - The page object and the base class are shown, not executed. They are the structure to copy into a real project; the test file is what runs here.
Framework
Six things to change, in order
Each one breaks something on purpose. Reading the failure is the skill the job pays for.
- Break a locatorChange
By.id("search-input")toBy.id("search-inpt"). Read which locator theNoSuchElementErrornames, and on which line. - Assert the wrong thingChange the expected cart value. The failure tells you what it wanted and what was actually there.
- Delete the waitRemove the
wait.until(...)before the first click and run. The product grid is drawn by JavaScript, so without the wait it is a race – which is exactly how flaky tests are born. - Loop over findElementsWrite a
for (WebElement e : driver.findElements(...))and count the visible ones withisDisplayed(). - Try an XPathReplace a CSS selector with
By.xpath("//a[contains(@href,'product')]"). It is real XPath against the real document. - Try something unsupportedAdd an
Actionschain. It will refuse and say so – that is the behaviour we want from a tool you are learning on.
Take it off this page
Playwright compiler
The same pane, the same application, in JavaScript and TypeScript – where your code really is executed.
A real Selenium project
The Serenity and hybrid BDD framework ZIPs, with the folder structure, pom.xml and reporting a real repository needs.
Java, properly
The forty-five Java coding challenges, and the Java Learning project behind them.