Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.50 KB | None | 0 0
  1. package com.mtvi.dte.tests.player.services.videoplayer.api;
  2.  
  3. import com.appium.driver.AppiumDriver;
  4. import com.common.exception.UnimplementedMethodException;
  5. import com.common.wait.FluentWait;
  6. import com.common.wait.Function;
  7. import com.common.wait.Sleeper;
  8. import com.mtvi.dte.tests.player.services.exception.PlayerToolbarException;
  9. import com.mtvi.dte.tests.player.services.exception.VideoNotLoadedException;
  10. import com.mtvi.dte.tests.player.services.videoplayer.constants.Operators;
  11. import com.mtvi.dte.tests.player.services.videoplayer.constants.PlayerPatternConstants;
  12. import com.mtvi.dte.tests.player.services.videoplayer.constants.PlayerSplitterConstants;
  13. import com.mtvi.dte.tests.player.services.videoplayer.constants.PlayerTimeConstants;
  14. import com.mtvi.dte.tests.player.services.videoplayer.element.IPlayerElement;
  15. import com.mtvi.dte.tests.player.services.videoplayer.util.PlayerUtils;
  16. import org.apache.log4j.Logger;
  17. import org.openqa.selenium.Dimension;
  18. import org.openqa.selenium.NotFoundException;
  19. import org.openqa.selenium.WebDriver;
  20. import org.openqa.selenium.WebElement;
  21.  
  22. import java.lang.reflect.UndeclaredThrowableException;
  23. import java.util.concurrent.TimeUnit;
  24.  
  25. public class MobileVideoPlayer extends AbstractPlayer {
  26.  
  27.     private static final Logger LOGGER = Logger.getLogger(MobileVideoPlayer.class);
  28.  
  29.     private IPlayerElement loadPlayerButton;
  30.  
  31.     private IPlayerElement playPauseButton;
  32.  
  33.     private IPlayerElement adView;
  34.  
  35.     private IPlayerElement closedCaptionsButton;
  36.  
  37.     private IPlayerElement fullScreenButton;
  38.  
  39.     private IPlayerElement bcVideoView;
  40.  
  41.     private IPlayerElement currentTime;
  42.  
  43.     private IPlayerElement endTime;
  44.  
  45.     private IPlayerElement spinner;
  46.  
  47.     private IPlayerElement prerollAdSpinner;
  48.  
  49.     private IPlayerElement scrubber;
  50.  
  51.     private IPlayerElement rewindButton;
  52.  
  53.     private IPlayerElement overlay;
  54.  
  55.     private IPlayerElement prepareVideoButton;
  56.  
  57.     private IPlayerElement prepareVideoField;
  58.  
  59.     private IPlayerElement configureVideoButton;
  60.  
  61.     private IPlayerElement configureVideoField;
  62.  
  63.     private IPlayerElement loadVideoButton;
  64.  
  65.     private IPlayerElement loadVideoField;
  66.  
  67.     private IPlayerElement loadVideoPlaylistButton;
  68.  
  69.     private IPlayerElement loadVideoPlaylistField;
  70.  
  71.     private IPlayerElement mediaApiTokenField;
  72.  
  73.     private IPlayerElement mrssToggle;
  74.  
  75.     private IPlayerElement chromelessAdsToggle;
  76.  
  77.     private IPlayerElement chromelessReportingToggle;
  78.  
  79.     private IPlayerElement chromelessClosedCaptonsToggle;
  80.  
  81.     private IPlayerElement chromelessPlayPauseButton;
  82.  
  83.     private IPlayerElement resetContentButton;
  84.  
  85.     private IPlayerElement saveStateButton;
  86.  
  87.     private IPlayerElement loadStateButton;
  88.  
  89.     private IPlayerElement liveStatus;
  90.  
  91.     private IPlayerElement adRemainTime;
  92.  
  93.     private IPlayerElement adPlayPauseButton;
  94.  
  95.     private IPlayerElement adFullScreenButton;
  96.  
  97.     private IPlayerElement adLearnMoreButton;
  98.  
  99.     private IPlayerElement adClosedCaptionsButton;
  100.  
  101.     public MobileVideoPlayer(WebDriver driver) {
  102.         super(driver);
  103.     }
  104.  
  105.     /**
  106.      * Get current time from video
  107.      *
  108.      */
  109.     @Override
  110.     public double getCurrentTimeInSec() {
  111.         String currentTimeValue = currentTime.getText();
  112.         if (currentTimeValue.matches(PlayerPatternConstants.IOS_TIME_PATTERN)) {
  113.             currentTimeValue = currentTimeValue.split(PlayerSplitterConstants.SLASH_SPLITTER)[0];
  114.         }
  115.         return PlayerUtils.convertStringTimeToDouble(currentTimeValue);
  116.  
  117.     }
  118.  
  119.     /**
  120.      * Get end time from video
  121.      *
  122.      */
  123.     @Override
  124.     public double getEndTimeInSec() {
  125.         if (null == endTime) {
  126.             return PlayerUtils.convertStringTimeToDouble(currentTime.getText().split(PlayerSplitterConstants.SLASH_SPLITTER)[1]);
  127.         }
  128.         String endTimeValue = endTime.getText();
  129.         return PlayerUtils.convertStringTimeToDouble(endTimeValue);
  130.  
  131.     }
  132.  
  133.     /**
  134.      * Get Remain time from Ad
  135.      *
  136.      */
  137.     public double getAdRemainTime() {
  138.         String adTimeValue = adRemainTime.getText();
  139.         return PlayerUtils.convertStringTimeToDouble(adTimeValue);
  140.     }
  141.  
  142.     /**
  143.      * Load player from initial screen
  144.      *
  145.      */
  146.     public void loadPlayer() {
  147.         LOGGER.info("Loading player");
  148.         loadPlayerButton.click();
  149.     }
  150.  
  151.     /**
  152.      * Open player toolbar
  153.      *
  154.      */
  155.     @Override
  156.     public void openPlayerToolbar() {
  157.         LOGGER.info("Opening toolbar");
  158.         waitForPlayerToolbarNotVisible(PlayerTimeConstants.SHORT_TIME_OUT);
  159.         bcVideoView.click();
  160.     }
  161.  
  162.     /**
  163.      * Open player toolbar for Ad
  164.      *
  165.      */
  166.     public void openAdPlayerToolbar() {
  167.         LOGGER.info("Opening toolbar");
  168.         waitForAdPlayerToolbarNotVisible(PlayerTimeConstants.SHORT_TIME_OUT);
  169.         bcVideoView.click();
  170.     }
  171.  
  172.     /**
  173.      * Play video in player
  174.      *
  175.      */
  176.     @Override
  177.     public void playVideo() {
  178.         LOGGER.info("Playing video");
  179.         openPlayerToolbar();
  180.         playPauseButton.click();
  181.     }
  182.  
  183.     /**
  184.      * Pause video in player
  185.      *
  186.      */
  187.     @Override
  188.     public void pauseVideo() {
  189.         LOGGER.info("Pausing video");
  190.         openPlayerToolbar();
  191.         playPauseButton.click();
  192.     }
  193.  
  194.     /**
  195.      * Toggle Closed Captions for video
  196.      *
  197.      */
  198.     @Override
  199.     public void toggleClosedCaptions() {
  200.         LOGGER.info("Toggle Closed Captions");
  201.         openPlayerToolbar();
  202.         closedCaptionsButton.click();
  203.     }
  204.  
  205.     /**
  206.      * Open Full Screen for video
  207.      *
  208.      * @throws RuntimeException
  209.      *
  210.      */
  211.     @Override
  212.     public void openFullScreen() {
  213.         if (!isFullScreen()) {
  214.             LOGGER.info("Open full screen mode in video player...");
  215.             openPlayerToolbar();
  216.             fullScreenButton.click();
  217.         }
  218.         if (!isFullScreen()) {
  219.             throw new RuntimeException("Fullscreen is not opened");
  220.         }
  221.     }
  222.  
  223.     /**
  224.      *
  225.      * Close Full Screen for video
  226.      *
  227.      * @throws RuntimeException
  228.      */
  229.     @Override
  230.     public void closeFullScreen() {
  231.         if (isFullScreen()) {
  232.             LOGGER.info("Open full screen mode in video player...");
  233.             openPlayerToolbar();
  234.             fullScreenButton.click();
  235.         }
  236.         if (isFullScreen()) {
  237.             throw new RuntimeException("Fullscreen is not closed");
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * Rewind video to start
  243.      *
  244.      */
  245.     public void rewindVideo() {
  246.         LOGGER.info("Rewinding video");
  247.         openPlayerToolbar();
  248.         rewindButton.click();
  249.     }
  250.  
  251.     /**
  252.      * Prepare video in player
  253.      *
  254.      */
  255.     public void prepareVideo(String mgid) {
  256.         LOGGER.info("Preparing video");
  257.         prepareVideoField.clear();
  258.         prepareVideoField.sendKeys(mgid);
  259.         prepareVideoButton.click();
  260.     }
  261.  
  262.     /**
  263.      * Configure video in player
  264.      *
  265.      */
  266.     public void configureVideo(String mgid) {
  267.         LOGGER.info("Configuring video");
  268.         configureVideoField.click();
  269.         configureVideoField.clear();
  270.         configureVideoField.sendKeys(mgid);
  271.         configureVideoButton.click();
  272.     }
  273.  
  274.     /**
  275.      * Load video in player
  276.      *
  277.      */
  278.     public void loadVideo(String mgid) {
  279.         if (mgid.matches(PlayerPatternConstants.VIDEO_CLIP_PATTERN)) {
  280.             LOGGER.info("Loading video clip");
  281.             loadVideoField.click();
  282.             loadVideoField.clear();
  283.             loadVideoField.sendKeys(mgid);
  284.             loadVideoButton.click();
  285.         } else if (mgid.matches(PlayerPatternConstants.FULL_EPISODE_PATTERN) || mgid.matches(PlayerPatternConstants.PLAYLIST_PATTERN)) {
  286.             LOGGER.info("Loading video full episode or playlist");
  287.             loadVideoPlaylistField.click();
  288.             loadVideoPlaylistField.clear();
  289.             loadVideoPlaylistField.sendKeys(mgid);
  290.             loadVideoPlaylistButton.click();
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * Type Media API token
  296.      *
  297.      */
  298.     public void typeMediaApiToken(String apiToken) {
  299.         LOGGER.info("Setting Media API Token ");
  300.         mediaApiTokenField.click();
  301.         mediaApiTokenField.clear();
  302.         mediaApiTokenField.sendKeys(apiToken);
  303.     }
  304.  
  305.     /**
  306.      * Toggle MRSS/VC
  307.      *
  308.      */
  309.     public void toggleMRSS() {
  310.         LOGGER.info("Toglle MRSS/VC mode");
  311.         mrssToggle.click();
  312.     }
  313.  
  314.     /**
  315.      * Toggle chromeless Ads
  316.      *
  317.      */
  318.     public void toggleCromelessAds() {
  319.         LOGGER.info("Toglle Ads through chromeless controls");
  320.         chromelessAdsToggle.click();
  321.     }
  322.  
  323.     /**
  324.      * Toggle chromeless Reporting
  325.      *
  326.      */
  327.     public void toggleCromelessReporting() {
  328.         LOGGER.info("Toglle Reporting through chromeless controls");
  329.         chromelessReportingToggle.click();
  330.     }
  331.  
  332.     /**
  333.      * Toggle chromeless Closed Captions
  334.      *
  335.      */
  336.     public void toggleCromelessClosedCaptions() {
  337.         LOGGER.info("Toggle Closed Caption through chromeless controls");
  338.         chromelessClosedCaptonsToggle.click();
  339.     }
  340.  
  341.     /**
  342.      * Toggle chromeless Play button
  343.      *
  344.      */
  345.     public void cromelessPlay() {
  346.         LOGGER.info("Playing video through chromeless controls");
  347.         chromelessPlayPauseButton.click();
  348.     }
  349.  
  350.     /**
  351.      * Toggle chromeless Pause button
  352.      *
  353.      */
  354.     public void cromelessPause() {
  355.         LOGGER.info("Pausing video through chromeless controls");
  356.         chromelessPlayPauseButton.click();
  357.     }
  358.  
  359.     /**
  360.      * Reset player content
  361.      *
  362.      */
  363.     public void resetContent() {
  364.         LOGGER.info("Reset player");
  365.         resetContentButton.click();
  366.     }
  367.  
  368.     /**
  369.      * Save player state
  370.      *
  371.      */
  372.     public void saveState() {
  373.         LOGGER.info("Saving player state");
  374.         saveStateButton.click();
  375.     }
  376.  
  377.     /**
  378.      * Load player state
  379.      *
  380.      */
  381.     public void loadState() {
  382.         LOGGER.info("Loading player state");
  383.         loadStateButton.click();
  384.     }
  385.  
  386.     /**
  387.      * Pause Ad on video
  388.      *
  389.      */
  390.     public void pauseAd() {
  391.         LOGGER.info("Pausing Ad");
  392.         openPlayerToolbar();
  393.         adPlayPauseButton.click();
  394.     }
  395.  
  396.     /**
  397.      * Play Ad on video
  398.      *
  399.      */
  400.     public void playAd() {
  401.         LOGGER.info("Playing Ad");
  402.         openPlayerToolbar();
  403.         adPlayPauseButton.click();
  404.     }
  405.  
  406.     /**
  407.      * Go to Learn more Ad
  408.      *
  409.      */
  410.     public void goLearnMore() {
  411.         LOGGER.info("Going to 'Learn More'");
  412.         openPlayerToolbar();
  413.         adLearnMoreButton.click();
  414.     }
  415.  
  416.     /**
  417.      * Toggle Ad Full Screen mode
  418.      *
  419.      */
  420.     public boolean toggleAdFullScreenMode() {
  421.         LOGGER.info("Toggle Full Screen mode");
  422.         Dimension before = adView.getSize();
  423.         openPlayerToolbar();
  424.         adFullScreenButton.click();
  425.         Dimension after = adView.getSize();
  426.         if (after.getWidth() > before.getWidth() || after.getHeight() > before.getHeight()) {
  427.             return true;
  428.         }
  429.         return false;
  430.     }
  431.  
  432.     /**
  433.      * Scrub video to define time
  434.      *
  435.      * @param pauseBeforeSeek
  436.      *            pause video before scrub
  437.      * @param timeToScroll
  438.      *            time to scroll
  439.      */
  440.     @Override
  441.     public void scrubVideo(boolean pauseBeforeSeek, double timeToScroll) {
  442.         float[] time = getTimeCoordinates(timeToScroll);
  443.         if (pauseBeforeSeek) {
  444.             pauseVideo();
  445.             dragScrubber(time);
  446.             playVideo();
  447.             waitForVideoTimeChanged();
  448.         } else {
  449.             dragScrubber(time);
  450.         }
  451.         Sleeper.SYSTEM_SLEEPER.sleep(5000);
  452.     }
  453.  
  454.     /**
  455.      * Scrub video to define time
  456.      *
  457.      * @param seconds
  458.      *            time to scroll
  459.      *
  460.      * @param adFinishTimeout
  461.      *
  462.      */
  463.     @Override
  464.     public void scrubVideo(double seconds, int adFinishTimeout) {
  465.  
  466.         throw new UnimplementedMethodException("Method scrubVideo() isn't implemented for mobile player");
  467.     }
  468.  
  469.     /**
  470.      * Scrub video to define time
  471.      *
  472.      * @param timeToScroll
  473.      *            time to scroll
  474.      */
  475.     @Override
  476.     public void scrubVideo(double timeToScroll) {
  477.         float[] time = getTimeCoordinates(timeToScroll);
  478.         dragScrubber(time);
  479.         Sleeper.SYSTEM_SLEEPER.sleep(5000);
  480.     }
  481.  
  482.     /**
  483.      * Calculate coordinates to seek
  484.      *
  485.      * @param timeInSec
  486.      *            time to scroll in seconds
  487.      */
  488.     private float[] getTimeCoordinates(double timeInSec) {
  489.         openPlayerToolbar();
  490.         WebElement seekBarLoaded = scrubber;
  491.         Dimension seekBarLoadedSize = seekBarLoaded.getSize();
  492.  
  493.         int endTimeValue = 0;
  494.         int seekBarWidth = 0;
  495.         float[] offset = new float[2];
  496.  
  497.         endTimeValue = (int) getEndTimeInSec();
  498.         if (endTimeValue < 0) {
  499.             LOGGER.info("Retry getTime method...");
  500.             endTimeValue = (int) getEndTimeInSec();
  501.         }
  502.  
  503.         seekBarWidth = seekBarLoadedSize.width;
  504.  
  505.         LOGGER.info("SeekBar width = " + seekBarWidth);
  506.         LOGGER.info("Calculate offset: (" + seekBarWidth + "*" + timeInSec + ")/" + endTimeValue + " ...");
  507.         float offsetValue = seekBarWidth * ((int) timeInSec) / endTimeValue;
  508.         LOGGER.info("Scrubber offset = " + offsetValue);
  509.         offset[0] = (float) seekBarLoaded.getLocation().x + offsetValue;
  510.         offset[1] = seekBarLoaded.getLocation().y;
  511.  
  512.         return offset;
  513.     }
  514.  
  515.     /**
  516.      * Touch to calculated coordinates for seeking
  517.      *
  518.      * @param offset
  519.      *            coordinates to seek
  520.      */
  521.     public void dragScrubber(float[] offset) {
  522.         openPlayerToolbar();
  523.         ((AppiumDriver) driver).touch((int) offset[0], (int) offset[1]);
  524.     }
  525.  
  526.     /**
  527.      * Toggle Ad Closed Captions
  528.      */
  529.     public void toggleAdClosedCaptions() {
  530.         LOGGER.info("Toggle Ad Closed Captions");
  531.         openPlayerToolbar();
  532.         adClosedCaptionsButton.click();
  533.     }
  534.  
  535.     /**
  536.      * Check video on full screen state
  537.      *
  538.      * @return return full screen state
  539.      */
  540.     @Override
  541.     public boolean isFullScreen() {
  542.  
  543.         Dimension viewDimension = bcVideoView.getSize();
  544.         LOGGER.info("View dimension is " + viewDimension);
  545.  
  546.         Dimension deviceDimension = driver.manage().window().getSize();
  547.         LOGGER.info("Device dimension is " + deviceDimension);
  548.  
  549.         if (deviceDimension.getWidth() > viewDimension.getWidth() || deviceDimension.getHeight() > viewDimension.getHeight()) {
  550.             return true;
  551.         }
  552.         return false;
  553.  
  554.     }
  555.  
  556.     /**
  557.      * Check Live status visibility
  558.      *
  559.      * @return Live status visibility
  560.      */
  561.     public boolean isLiveStatusVisible() {
  562.         try {
  563.             LOGGER.info("Checking Live Status visibility");
  564.             return liveStatus.isDisplayed();
  565.         } catch (NotFoundException e) {
  566.             return false;
  567.         }
  568.     }
  569.  
  570.     /**
  571.      * Check Ad visibility
  572.      *
  573.      * @return Ad visibility
  574.      */
  575.     @Override
  576.     public boolean isAdDisplayed() {
  577.         try {
  578.             LOGGER.info("Checking Ad View visibility");
  579.             return adView.isDisplayed();
  580.         } catch (NotFoundException e) {
  581.             return false;
  582.         }
  583.     }
  584.  
  585.     /**
  586.      * Check progress bar visibility
  587.      *
  588.      * @return progress bar visibility
  589.      */
  590.     @Override
  591.     public boolean isProgressBarVisible() {
  592.         LOGGER.info("Checking progress bar visibility");
  593.         try {
  594.             return scrubber.isDisplayed();
  595.         } catch (NotFoundException e) {
  596.             return false;
  597.         }
  598.     }
  599.  
  600.     /**
  601.      * Check Rewind button visibility
  602.      *
  603.      * @return Rewind button visibility
  604.      */
  605.     public boolean isRewindButtonVisible() {
  606.         LOGGER.info("Checking Rewind Button visibility");
  607.         try {
  608.             return rewindButton.isDisplayed();
  609.         } catch (NotFoundException e) {
  610.             return false;
  611.         }
  612.     }
  613.  
  614.     /**
  615.      * Check Spinner visibility
  616.      *
  617.      * @return Spinner visibility
  618.      */
  619.     public boolean isSpinnerDisplayed() {
  620.         LOGGER.info("Checking Spinner visibility");
  621.         try {
  622.             return spinner.isDisplayed();
  623.         } catch (NotFoundException e) {
  624.             return false;
  625.         }
  626.     }
  627.  
  628.     /**
  629.      * Check Preroll Ad Spinner visibility
  630.      *
  631.      * @return preroll Ad Spinner visibility
  632.      */
  633.     public boolean isPrerollAdSpinnerDisplayed() {
  634.         LOGGER.info("Checking Spinner visibility");
  635.         try {
  636.             return prerollAdSpinner.isDisplayed();
  637.         } catch (NotFoundException e) {
  638.             return false;
  639.         }
  640.     }
  641.  
  642.     /**
  643.      * Check Play/Pause button visibility
  644.      *
  645.      * @return Play/Pause button visibility
  646.      */
  647.     public boolean isPlayPauseButtonDisplayed() {
  648.         LOGGER.info("Checking Play Pause Button visibility");
  649.         try {
  650.             return playPauseButton.isDisplayed();
  651.         } catch (NotFoundException e) {
  652.             return false;
  653.         }
  654.     }
  655.  
  656.     /**
  657.      * Check Play button visibility
  658.      *
  659.      * @return Play button visibility
  660.      */
  661.     @Override
  662.     public boolean isPlayButtonDisplayed() {
  663.         return isPlayPauseButtonDisplayed();
  664.     }
  665.  
  666.     /**
  667.      * Check Pause button visibility
  668.      *
  669.      * @return Pause button visibility
  670.      */
  671.     @Override
  672.     public boolean isPauseButtonDisplayed() {
  673.         return isPlayPauseButtonDisplayed();
  674.     }
  675.  
  676.     /**
  677.      * Check Closed Captions button visibility
  678.      *
  679.      * @return Closed Captions button visibility
  680.      */
  681.     @Override
  682.     public boolean isClosedCaptionsButtonDisplayed() {
  683.         try {
  684.             LOGGER.info("Checking Closed Captions Button visibility");
  685.             return closedCaptionsButton.isDisplayed();
  686.         } catch (NotFoundException e) {
  687.             return false;
  688.         }
  689.     }
  690.  
  691.     /**
  692.      * Check Full Screen button visibility
  693.      *
  694.      * @return Full Screen button visibility
  695.      */
  696.     public boolean isFullScreenButtonDisplayed() {
  697.         try {
  698.             LOGGER.info("Checking Full Screen Button visibility");
  699.             return fullScreenButton.isDisplayed();
  700.         } catch (NotFoundException e) {
  701.             return false;
  702.         }
  703.     }
  704.  
  705.     @Override
  706.     public boolean isEnterFullScreenButtonDisplayed() {
  707.         return isFullScreenButtonDisplayed();
  708.     }
  709.  
  710.     @Override
  711.     public boolean isCloseFullScreenButtonDisplayed() {
  712.         return isFullScreenButtonDisplayed();
  713.     }
  714.  
  715.     /**
  716.      * Check Current time visibility
  717.      *
  718.      * @return Current time visibility
  719.      */
  720.     @Override
  721.     public boolean isCurrentTimeDisplayed() {
  722.         try {
  723.             LOGGER.info("Checking Current Time visibility");
  724.             return currentTime.isDisplayed();
  725.         } catch (NotFoundException e) {
  726.             return false;
  727.         }
  728.     }
  729.  
  730.     /**
  731.      * Check End time visibility
  732.      *
  733.      * @return End time visibility
  734.      */
  735.     @Override
  736.     public boolean isEndTimeDisplayed() {
  737.         try {
  738.             LOGGER.info("Checking End Time visibility");
  739.             return endTime.isDisplayed();
  740.         } catch (NotFoundException e) {
  741.             return false;
  742.         }
  743.     }
  744.  
  745.     /**
  746.      * Check Ad Learn More button visibility
  747.      *
  748.      * @return Ad Learn More button visibility
  749.      */
  750.     public boolean isAdLearnMoreButtonDisplayed() {
  751.         try {
  752.             LOGGER.info("Checking Ad Learn More button visibility");
  753.             return adLearnMoreButton.isDisplayed();
  754.         } catch (NotFoundException e) {
  755.             return false;
  756.         }
  757.     }
  758.  
  759.     /**
  760.      * Check Ad Closed Captions button visibility
  761.      *
  762.      * @return Ad Closed Captions button visibility
  763.      */
  764.     public boolean isAdClosedCaptionsButtonDisplayed() {
  765.         try {
  766.             LOGGER.info("Checking Ad Learn More button visibility");
  767.             return adClosedCaptionsButton.isDisplayed();
  768.         } catch (NotFoundException e) {
  769.             return false;
  770.         }
  771.     }
  772.  
  773.     /**
  774.      * Check Ad Play/Pause button visibility
  775.      *
  776.      * @return Ad Play/Pause button visibility
  777.      */
  778.     public boolean isAdPlayPauseButtonDisplayed() {
  779.         try {
  780.             LOGGER.info("Checking Ad Learn More button visibility");
  781.             return adPlayPauseButton.isDisplayed();
  782.         } catch (NotFoundException e) {
  783.             return false;
  784.         }
  785.     }
  786.  
  787.     /**
  788.      * Check Ad Full Screen button visibility
  789.      *
  790.      * @return Ad Full Screen button visibility
  791.      */
  792.     public boolean isAdFullScreenButtonDisplayed() {
  793.         try {
  794.             LOGGER.info("Checking Ad Learn More button visibility");
  795.             return adFullScreenButton.isDisplayed();
  796.         } catch (NotFoundException e) {
  797.             return false;
  798.         }
  799.     }
  800.  
  801.     /**
  802.      * Check on Ad pause
  803.      *
  804.      * @param sleepTimeSec
  805.      *            time out value
  806.      * @return Ad pause status
  807.      */
  808.     @Override
  809.     public boolean isAdPaused(int sleepTimeSec) {
  810.         int timeBefore = (int) getAdRemainTime();
  811.         Sleeper.SYSTEM_SLEEPER.sleep(sleepTimeSec * 1000);
  812.         int timeAfter = (int) getAdRemainTime();
  813.         LOGGER.info("Time before pause: " + timeBefore + "\n" + "Time after 3 sec of pause: " + timeAfter);
  814.  
  815.         return timeBefore == timeAfter;
  816.     }
  817.  
  818.     /**
  819.      * Check on Ad play
  820.      *
  821.      * @param sleepTimeSec
  822.      *            time out value
  823.      * @return Ad play status
  824.      */
  825.     @Override
  826.     public boolean isAdPlaying(int sleepTimeSec) {
  827.         int timeBefore = (int) getAdRemainTime();
  828.         Sleeper.SYSTEM_SLEEPER.sleep(sleepTimeSec * 1000);
  829.         int timeAfter = (int) getAdRemainTime();
  830.         LOGGER.info("Time before pause: " + timeBefore + "\n" + "Time after 3 sec of pause: " + timeAfter);
  831.  
  832.         return timeBefore < timeAfter;
  833.     }
  834.  
  835.     /**
  836.      * Waiting for next video will be played
  837.      *
  838.      * @param prerollTime
  839.      *            preroll time value
  840.      * @param previousVideoTime
  841.      *            previous time value
  842.      */
  843.     public void waitForNextVideoPlayed(int prerollTime, int previousVideoTime) {
  844.         LOGGER.info("Check next video played");
  845.         waitForVideoTime(10, PlayerTimeConstants.VIDEO_LOAD_TIMEOUT_SEC, Operators.LESS_EQUAL);
  846.         if (isAdDisplayed()) {
  847.             waitForAdStart(PlayerTimeConstants.SHORT_TIME_OUT);
  848.             waitForAdEnded(prerollTime);
  849.         }
  850.     }
  851.  
  852.     /**
  853.      * Waiting for video time or video end
  854.      *
  855.      * @param time
  856.      * @param timeoutSec
  857.      * @return real time when method stop to wait
  858.      *
  859.      * @throws VideoNotLoadedException
  860.      */
  861.     public int waitForVideoTimeOrVideoEnd(final int time, int timeoutSec) {
  862.  
  863.         LOGGER.info("Wait for Video time = " + time + " or video is ended");
  864.         Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>() {
  865.             int currentTime;
  866.  
  867.             @Override
  868.             public Boolean apply(WebDriver input) {
  869.                 try {
  870.                     currentTime = (int) getCurrentTimeInSec();
  871.                 } catch (UndeclaredThrowableException e) {
  872.                     LOGGER.info("Video is ended.");
  873.                     currentTime = 0;
  874.                     return true;
  875.                 }
  876.                 int timeToWait = time - currentTime;
  877.                 LOGGER.info("Time to wait: " + timeToWait);
  878.                 return (timeToWait <= 0) || (currentTime == 0) || (timeToWait == 1);
  879.             }
  880.  
  881.             public String toString() {
  882.                 return Integer.toString(currentTime);
  883.             }
  884.  
  885.         };
  886.  
  887.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  888.                 .until(function, VideoNotLoadedException.class, "Video time still not " + time);
  889.  
  890.         int timeValue = Integer.parseInt(function.toString());
  891.         LOGGER.info("Time: " + timeValue);
  892.         return timeValue;
  893.     }
  894.  
  895.     /**
  896.      * Waiting for video time or video end
  897.      *
  898.      * @param time
  899.      * @return real time when method stop to wait
  900.      */
  901.  
  902.     public int waitForVideoTimeOrVideoEnd(final int time) {
  903.         LOGGER.info("Wait for Video time = " + time + " or video is ended");
  904.         int currentTime = (int) getCurrentTimeInSec();
  905.  
  906.         int timeout = PlayerUtils.calculateWaitTime(currentTime, time, PlayerTimeConstants.MULTIPLICATOR);
  907.         return waitForVideoTimeOrVideoEnd(time, timeout);
  908.     }
  909.  
  910.     /**
  911.      * Waiting for video time or video end
  912.      *
  913.      * @param time
  914.      * @return real time when method stop to wait
  915.      */
  916.     public int waitForVideoTimeFromTo(int startTime, int time) {
  917.         LOGGER.info("Wait for Video time = " + time + " or video is ended");
  918.         int timeout = PlayerUtils.calculateWaitTime(startTime, time, PlayerTimeConstants.MULTIPLICATOR);
  919.         return waitForVideoTimeOrVideoEnd(time, timeout);
  920.     }
  921.  
  922.     /**
  923.      * Waiting for remain ad time was changed
  924.      *
  925.      * @param timeoutSec
  926.      */
  927.     public void waitForRemainTimeChanged(int timeoutSec) {
  928.         LOGGER.info("Wait for Remain time changed");
  929.         final String startTime = adRemainTime.getText();
  930.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  931.                 .until(new Function<WebDriver, Boolean>() {
  932.                     @Override
  933.                     public Boolean apply(WebDriver input) {
  934.                         return !startTime.equals(adRemainTime.getText());
  935.                     }
  936.                 }, VideoNotLoadedException.class, "Video didn't start");
  937.  
  938.     }
  939.  
  940.     /**
  941.      * Waiting for Ad will be started
  942.      *
  943.      * @param timeout
  944.      *            timeout value in seconds
  945.      */
  946.     @Override
  947.     public void waitForAdStart(int timeout) {
  948.         LOGGER.info("Wait for Ad message...");
  949.         new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  950.                 .until(new Function<WebDriver, Boolean>() {
  951.                     @Override
  952.                     public Boolean apply(WebDriver input) {
  953.                         return adRemainTime.isDisplayed();
  954.                     }
  955.                 });
  956.     }
  957.  
  958.     /**
  959.      * Waiting for Ad will be ended
  960.      *
  961.      * @param adTimeout
  962.      *            timeout value in seconds
  963.      */
  964.     @Override
  965.     public void waitForAdEnded(int adTimeout) {
  966.         LOGGER.info("Wait for Ad message...");
  967.         new FluentWait<WebDriver>(driver).withTimeout(adTimeout, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  968.                 .until(new Function<WebDriver, Boolean>() {
  969.                     @Override
  970.                     public Boolean apply(WebDriver input) {
  971.                         return !adRemainTime.isDisplayed();
  972.                     }
  973.                 });
  974.     }
  975.  
  976.     /**
  977.      * Waiting for player toolbar will be hidden
  978.      *
  979.      * @param timeoutSec
  980.      *            timeout value in seconds
  981.      */
  982.     @Override
  983.     public void waitForPlayerToolbarNotVisible(int timeoutSec) {
  984.         LOGGER.info("Wait for player toolbar isn't visible ...");
  985.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  986.                 .until(new Function<WebDriver, Boolean>() {
  987.                     @Override
  988.                     public Boolean apply(WebDriver input) {
  989.                         return !playPauseButton.isDisplayed();
  990.                     }
  991.                 }, PlayerToolbarException.class, "ERROR! Player toolbar is not hidden");
  992.     }
  993.  
  994.     /**
  995.      * Waiting for player toolbar for Ad will be hidden
  996.      *
  997.      * @param timeoutSec
  998.      *            timeout value in seconds
  999.      */
  1000.     public void waitForAdPlayerToolbarNotVisible(int timeoutSec) {
  1001.         LOGGER.info("Wait for player toolbar isn't visible ...");
  1002.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS)
  1003.                 .until(new Function<WebDriver, Boolean>() {
  1004.                     @Override
  1005.                     public Boolean apply(WebDriver input) {
  1006.                         return !adPlayPauseButton.isDisplayed();
  1007.                     }
  1008.                 }, PlayerToolbarException.class, "ERROR! Player toolbar is not hidden");
  1009.     }
  1010.  
  1011.     /**
  1012.      * Waiting for overlay will be visible
  1013.      *
  1014.      */
  1015.     public void waitForOverlay() {
  1016.         LOGGER.info("Wait for player toolbar isn't visible ...");
  1017.         new FluentWait<WebDriver>(driver).withTimeout(PlayerTimeConstants.OVERLAY_LOAD_TIMEOUT_SEC, TimeUnit.SECONDS)
  1018.                 .pollingEvery(1, TimeUnit.SECONDS).until(new Function<WebDriver, Boolean>() {
  1019.                     @Override
  1020.                     public Boolean apply(WebDriver input) {
  1021.                         return overlay.isDisplayed();
  1022.                     }
  1023.                 }, RuntimeException.class, "ERROR! Overlay is hidden");
  1024.     }
  1025.  
  1026.     /**
  1027.      * Waiting for overlay will be hidden
  1028.      *
  1029.      * @return time when overlay hidden
  1030.      */
  1031.     public long getOverlayDisappearTime() {
  1032.         new FluentWait<WebDriver>(driver).withTimeout(PlayerTimeConstants.OVERLAY_LOAD_TIMEOUT_SEC, TimeUnit.SECONDS).until(
  1033.                 new Function<WebDriver, Boolean>() {
  1034.                     @Override
  1035.                     public Boolean apply(WebDriver input) {
  1036.                         return !overlay.isDisplayed();
  1037.                     }
  1038.                 }, RuntimeException.class, "Overlay not dissapeared");
  1039.         return System.currentTimeMillis();
  1040.     }
  1041.  
  1042.     /**
  1043.      * Waiting for spinner will be visible
  1044.      *
  1045.      * @return time when spinner visible
  1046.      */
  1047.     public void waitForSpinnerVisible(long timeoutSec) {
  1048.         LOGGER.info("Waiting for spinner visibility during " + timeoutSec);
  1049.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).until(new Function<WebDriver, Boolean>() {
  1050.             @Override
  1051.             public Boolean apply(WebDriver input) {
  1052.                 return spinner.isDisplayed();
  1053.             }
  1054.         }, RuntimeException.class, "ERROR! Spinner is not displayed (*UNSTABLE*)");
  1055.  
  1056.     }
  1057.  
  1058.     /**
  1059.      * Waiting for spinner will be hidden
  1060.      *
  1061.      * @return time when spinner hidden
  1062.      */
  1063.     public void waitForSpinnerHidden(long timeoutSec) {
  1064.         LOGGER.info("Waiting for spinner disappearing during " + timeoutSec);
  1065.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).until(new Function<WebDriver, Boolean>() {
  1066.             @Override
  1067.             public Boolean apply(WebDriver input) {
  1068.                 return !spinner.isDisplayed();
  1069.             }
  1070.         }, RuntimeException.class, "ERROR! Spinner is not hidden (*UNSTABLE*)");
  1071.     }
  1072.  
  1073.     /**
  1074.      * Waiting for preroll Ad spinner will be visible
  1075.      *
  1076.      * @return time when preroll Ad spinner visible
  1077.      */
  1078.     public void waitForPrerollAdSpinnerVisible(long timeoutSec) {
  1079.         LOGGER.info("Waiting for pre-roll Ad spinner visibility during " + timeoutSec);
  1080.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).until(new Function<WebDriver, Boolean>() {
  1081.             @Override
  1082.             public Boolean apply(WebDriver input) {
  1083.                 return prerollAdSpinner.isDisplayed();
  1084.             }
  1085.         }, RuntimeException.class, "ERROR! Spinner is not hidden (*UNSTABLE*)");
  1086.     }
  1087.  
  1088.     /**
  1089.      * Waiting for preroll Ad spinner will be hidden
  1090.      *
  1091.      * @return time when preroll Ad spinner hidden
  1092.      */
  1093.     public void waitForPrerollAdSpinnerHidden(long timeoutSec) {
  1094.         LOGGER.info("Waiting for pre-roll Ad spinner disappearing during " + timeoutSec);
  1095.         new FluentWait<WebDriver>(driver).withTimeout(timeoutSec, TimeUnit.SECONDS).until(new Function<WebDriver, Boolean>() {
  1096.             @Override
  1097.             public Boolean apply(WebDriver input) {
  1098.                 return !prerollAdSpinner.isDisplayed();
  1099.             }
  1100.         }, RuntimeException.class, "ERROR! Spinner is not hidden (*UNSTABLE*)");
  1101.  
  1102.     }
  1103.  
  1104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement