简体   繁体   English

如何使用Java使用Selenium WebDriver验证动态库?

[英]How to verify dynamic gallery with Selenium WebDriver using Java?

Problem: 问题:

The image in the gallery is always dynamic, but the article keeps changing. 画廊中的图像始终是动态的,但文章一直在变化。 When I click on the article the image gallery open with a big image and other thumbnails with a carousel. 当我单击该文章时,图像库中会打开一个大图像,而其他缩略图会显示一个轮播。


First Scenario : 第一种情况:

I need to verify that the image is loaded and the gallery is fine 我需要验证图像是否已加载并且图库是否正常 . Also the other thumbnail images has to be navigated and verified.` 另外,其他缩略图图像也必须导航和验证。

Example: 例:

    1. I am on a article page and i open that article, so this article page has an Gallery opened with > 5 thumbnails images. 我在文章页面上,并且打开了该文章,因此该文章页面上有一个图库,其中包含> 5张缩略图图像。
    1. Now I need to verify that the image is opened and also navigate one after the another to verify the thumbnails as well. 现在,我需要验证图像是否已打开,并且还要一个接一个地导航以验证缩略图。

2nd Scenario : 第二种情况:

Is some times this article will have image loaded ,were has sometimes it can have video's or just a single image. 有时本文会加载图像,有时会包含视频或仅包含一个图像。 So that's what i meant to be it as Dynamic.` 所以这就是我要成为动态的意思。

So each time the image change,need to know how to handle this situation Below is the html code: 所以每次图像更改时,都需要知道如何处理这种情况,下面是html代码:

<div class="carousel royalSlider rsDefault rsAutoHeight rsHor  rsWithThumbs rsWithThumbsHor" style="min-height: 463.8px;">
    <div class="rsOverflow" style="width: 773px; height: 436px; transition:  height 600ms ease-in-out;">
        <div class="rsContainer" style="transition-duration: 600ms; transform:  translate3d(-797px, 0px, 0px); transition-timing-function: cubic-bezier(0.445,  0.05, 0.55, 0.95);">
            <div style="left: 0px;" class="rsSlide"><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (1).jpg"></div>
            <div style="left: 797px;" class="rsSlide rsActiveSlide"><img  class="rsImg rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (2).jpg"></div>
            <div style="left: 1594px;" class="rsSlide "><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (3).jpg"></div>
            <div style="left: 2391px;" class="rsSlide "><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (4).jpg"></div>
        </div>
        <div class="rsFullscreenBtn">
            <div class="rsFullscreenIcn"></div>
        </div>
        <div class="rsArrow rsArrowLeft" style="display: block;">
            <div class="rsArrowIcn"></div>
        </div>
        <div class="rsArrow rsArrowRight" style="display: block;">
            <div class="rsArrowIcn"></div>
        </div>
        <div class="rsGCaption">Lexus RX 350 F Sport.<noscript>&lt;img  src=http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg&gt;</noscript></div>
    </div>
    <div class="rsNav rsThumbs rsThumbsHor">
        <div class="rsThumbsContainer" style="transition-property: -webkit- transform; transform: translate3d(86.5px, 0px, 0px); width: 612px;">
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(1).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(1).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb  rsNavSelected"><img class="rsTmb lazyload"  onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img c lass="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(3).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(3).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(4).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(4).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(5).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(5).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(6).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(6).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
        </div>
        <div class="rsThumbsArrow rsThumbsArrowLeft  rsThumbsArrowDisabled">
            <div class="rsThumbsArrowIcn"></div>
        </div>
        <div class="rsThumbsArrow rsThumbsArrowRight rsThumbsArrowDisabled">
            <div class="rsThumbsArrowIcn"></div>
        </div>
    </div>
    <div style="clear:both; float: none;"></div>
</div>

All I have done so far is : 到目前为止,我所做的只是:

    1. Open the URL. 打开URL。
    1. Click on the Article page. 单击文章页面。
    1. Now open that / any article. 现在打开该/任何文章。

My 4th step would be to verify that image (with thumbnails) when the article opens. 我的第四步是在打开文章时验证该图像(带有缩略图)。 I am not able to do that 4th step. 我无法执行第4步。 I don't have any error now, I am able to do it successfully till step 3. 我现在没有任何错误,直到第3步我都可以成功完成。

Hi please do it like below 嗨,请像下面这样

public class WorkingWithThumbNails {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.carsguide.com.au/car-reviews/toyota-tarago-used-review-1990-2015-41984");

    // Now count the number of images in the thumb-nails cause
    // this will decide number of clicks on the next Arrow

    List<WebElement> myThumbnails = driver.findElements(By.cssSelector(".rsTmb.lazyload"));
    List<WebElement> myMainImages = driver.findElements(By.cssSelector(".rsImg.rsMainSlideImage"));

    System.out.println("Size of the thumbnail is : " + myThumbnails.size());
    System.out.println("Size of the main image is : " + myMainImages.size());


    // print the urls of the thumb-nails as well
    for(int i=0;i< myThumbnails.size();i++){
        myThumbnails = driver.findElements(By.cssSelector(".rsTmb.lazyload"));
        System.out.println("Url for the Thumbnail is : " + myThumbnails.get(i).getAttribute("src"));

        myMainImages = driver.findElements(By.cssSelector(".rsImg.rsMainSlideImage"));
        System.out.println("Url for the main image is : " + myMainImages.get(i).getAttribute("src"));

        // now implement the logic of img verification, logic can be many but here i used if src for bigger img 
        // contains img name as per the thumbnail img name then its ok img is properly loaded and hence it is right image.

        String[] myMainImgName = myMainImages.get(i).getAttribute("src").split("s3");

        if(myThumbnails.get(i).getAttribute("src").contains(myMainImgName[1])){
            System.out.println("Main image is as per the thumbnail.");
            // to verify next thumb-nail img with main img we have to click
            // here if is because loop will run one more time then img size
            if(driver.findElements(By.xpath("//*[@class='rsArrow rsArrowRight']/div")).size() > 0){
                driver.findElement(By.xpath("//*[@class='rsArrow rsArrowRight']/div")).click();
            }
        }else{
            // apply the same logic for videos 
            // hope you can do this 
        }
    }

}

} }

Please feel free to ask query if any. 请随时询问是否有疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java验证Selenium WebDriver中的tooltipText - How to verify tooltipText in Selenium WebDriver using Java 如何使用Java使用Selenium WebDriver验证是否单击了按钮 - How to verify whether a Button is clicked or not with Selenium WebDriver using Java 如何使用Java在Selenium Webdriver中验证单词是否为粗体? - How to verify that a word is Bold in selenium webdriver using Java? 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如果对象在Selenium Webdriver Java中不可见,如何使用xpath验证对象 - how to verify object using xpath if object is invisible in selenium webdriver java 使用Java在Selenium WebDriver中发布后,每次如何验证文本? - How to verify text everytime after posting with Selenium WebDriver using java? 如何在Selenium Java Webdriver中验证图像的尺寸? - How to verify dimensions of image in Selenium Java Webdriver? 如何使用Java + Selenium WebDriver移动到动态页面的底部 - How to move to bottom of the dynamic page using Java + Selenium WebDriver 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 如何在Selenium WebDriver中验证动态下拉值? - How to verify Dynamic drop down values in Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM