|
| 1 | +package com.saucelabs.appium.page_object.widgets; |
| 2 | + |
| 3 | +import io.appium.java_client.pagefactory.AppiumFieldDecorator; |
| 4 | +import io.appium.java_client.pagefactory.TimeOutDuration; |
| 5 | +import org.apache.commons.io.FileUtils; |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | +import org.openqa.selenium.NoSuchElementException; |
| 12 | +import org.openqa.selenium.Platform; |
| 13 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 14 | +import org.openqa.selenium.chrome.ChromeDriverService; |
| 15 | +import org.openqa.selenium.support.PageFactory; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +public class HtmlOverrideWidgetTest implements WidgetTest{ |
| 24 | + |
| 25 | + private static ChromeDriver driver; |
| 26 | + private static RottenTomatoes rottenTomatoes; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void beforeClass() throws Exception { |
| 30 | + |
| 31 | + if (Platform.getCurrent().is(Platform.WINDOWS)) { |
| 32 | + FileUtils.copyFile(new File("../../../apps/chromedriverWin"), new File("../../../apps/chromedriver.exe")); |
| 33 | + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, |
| 34 | + new File("../../../apps/chromedriver.exe").getAbsolutePath()); |
| 35 | + } |
| 36 | + else { |
| 37 | + System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, |
| 38 | + new File("../../../apps/chromedriver").getAbsolutePath()); |
| 39 | + } |
| 40 | + |
| 41 | + driver = new ChromeDriver(); |
| 42 | + driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); |
| 43 | + rottenTomatoes = new RottenTomatoes(); |
| 44 | + PageFactory.initElements(new AppiumFieldDecorator(driver, new TimeOutDuration(5, TimeUnit.SECONDS)), rottenTomatoes); |
| 45 | + } |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() throws Exception { |
| 49 | + if (driver != null) |
| 50 | + driver.get("file:///" + new File("../../../apps/RottenTomatoesSnapshot.html").getAbsolutePath()); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterClass |
| 54 | + public static void afterClass() throws Exception { |
| 55 | + if (driver != null) |
| 56 | + driver.quit(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @Override |
| 61 | + public void checkACommonWidget() { |
| 62 | + assertTrue(rottenTomatoes.getSimpleMovieCount() >= 1); |
| 63 | + Movie movie = rottenTomatoes.getASimpleMovie(0); |
| 64 | + assertTrue(!StringUtils.isBlank(movie.title())); |
| 65 | + assertTrue(!StringUtils.isBlank(movie.score())); |
| 66 | + assertNotNull(movie.getPoster()); |
| 67 | + movie.goToReview(); |
| 68 | + |
| 69 | + rottenTomatoes.checkSimpleReview(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + @Test |
| 74 | + public void checkAnAnnotatedWidget() { |
| 75 | + assertTrue(rottenTomatoes.getAnnotatedMovieCount() >= 1); |
| 76 | + Movie movie = rottenTomatoes.getAnAnnotatedMovie(0); |
| 77 | + assertTrue(!StringUtils.isBlank(movie.title())); |
| 78 | + assertTrue(!StringUtils.isBlank(movie.score())); |
| 79 | + assertNotNull(movie.getPoster()); |
| 80 | + movie.goToReview(); |
| 81 | + |
| 82 | + rottenTomatoes.checkAnnotatedReview(); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + @Override |
| 87 | + @Test |
| 88 | + public void checkAnExtendedWidget() { |
| 89 | + assertTrue(rottenTomatoes.getExtendeddMovieCount() >= 1); |
| 90 | + Movie movie = rottenTomatoes.getAnExtendedMovie(0); |
| 91 | + assertTrue(!StringUtils.isBlank(movie.title())); |
| 92 | + assertTrue(!StringUtils.isBlank(movie.score())); |
| 93 | + assertNotNull(movie.getPoster()); |
| 94 | + movie.goToReview(); |
| 95 | + |
| 96 | + rottenTomatoes.checkExtendedReview(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + @Test |
| 101 | + public void checkTheLocatorOverridingOnAWidget() { |
| 102 | + try { |
| 103 | + assertTrue(rottenTomatoes.getFakedMovieCount() == 0); |
| 104 | + } |
| 105 | + catch (Exception e){ |
| 106 | + if (!NoSuchElementException.class.isAssignableFrom(e.getClass())) |
| 107 | + throw e; |
| 108 | + } |
| 109 | + |
| 110 | + rottenTomatoes.getASimpleMovie(0).goToReview(); |
| 111 | + |
| 112 | + try { |
| 113 | + rottenTomatoes.checkFakeReview(); |
| 114 | + } |
| 115 | + catch (Exception e){ |
| 116 | + if (NoSuchElementException.class.isAssignableFrom(e.getClass())) |
| 117 | + return; |
| 118 | + else |
| 119 | + throw e; |
| 120 | + } |
| 121 | + throw new RuntimeException("Any exception was expected"); |
| 122 | + } |
| 123 | +} |
0 commit comments