GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — HowToWebDriver/src/examples/ex8_TestThings.java at master · LeHack/HowToWebDriver Expand file tree/ Copy pathex8_TestThings.javaMore file actions 135 lines (119 loc) · 4.4 KB / Copy pathex8_TestThings.javaFile metadata and controls135 lines (119 loc) · 4.4 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 package examples; import java.io.File; import java.io.IOException; import java.util.List; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.ITestResult; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; import org.testng.annotations.AfterMethod; import utils.Config; /** * You need to have TestNG plugin installed in Eclipse to run this. * Try Help -> Install new software and sitename: http://beust.com/eclipse * Then use Run As -> TestNG Test * * Note: make sure to checkout the getScreenshot() and cleanup() methods */ public class ex8_TestThings { private static final Logger logger = Config.getLogger(ex8_TestThings.class); private final WebDriver drv; public ex8_TestThings() { Config cfg = new Config(); // Look at util.Config.createInstance for details this.drv = cfg.createInstance(); this.drv.navigate().to(cfg.get("site_url")); logger.info("At the test site..."); } @Test public void testWelcome() { WebElement elem = this.drv.findElement(By.cssSelector("h1")); Assert.assertEquals(elem.getText(), "Welcome", "Correct greeting"); } @Test public void testShowingKitty() { /* * we don't know the order of the tests, so we must assume * a Schrödinger cat scenario and handle both cases */ List<WebElement> kittyCheck = this.drv.findElements(By.id("kitty")); if (!kittyCheck.isEmpty()) { // if Kitty is already there, hide him ((JavascriptExecutor)this.drv).executeScript("Kitty.hide()"); } // now do the actual test try { WebElement button = drv.findElement(By.cssSelector(".kittyStart input")); button.click(); } catch (NoSuchElementException e) { Assert.fail("Kitty showing button not found"); } WebElement kitty = this.drv.findElement(By.id("kitty")); Assert.assertTrue(kitty.getCssValue("background-image").contains("kitty.png"), "Looks like Kitty"); } @Test public void testHidingKitty() { /* * Again: * we don't know the order of the tests, so we must assume * a Schrödinger cat scenario and handle both cases */ List<WebElement> kittyCheck = this.drv.findElements(By.id("kitty")); if (kittyCheck.isEmpty()) { // if Kitty is not already there, show him ((JavascriptExecutor)this.drv).executeScript("Kitty.show()"); } // now do the actual test try { WebElement button = drv.findElement(By.cssSelector(".kittyStart input")); button.click(); } catch (NoSuchElementException e) { Assert.fail("Kitty hiding button not found"); } try { this.drv.findElement(By.id("kitty")); Assert.fail("Not good, Kitty was found on site."); } catch (NoSuchElementException e) { Assert.assertTrue(true, "Kitty was not found, as expected."); } } @Test public void testWhichFails() { Assert.fail("Just because"); } @AfterClass protected void cleanup() { // make sure we close WebDriver after TestNG finishes this.drv.close(); } @AfterMethod(alwaysRun=true) protected void getScreenshot(ITestResult result) { if (result.isSuccess()) return; // skip, if the test passed // only failed tests will run this part File scrFile = ((TakesScreenshot) this.drv).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(scrFile, new File("screenshot.jpg"), true); } catch (IOException e) { logger.warning("Cannot create screenshot file."); } /* * Now go see the screenshot.jpg in your main project directory. * Note that only a portion of the page may be visible as * Selenium tries to detect the overall dimensions of the web page. * Read https://stackoverflow.com/a/44086163 for details. */ } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |