簡體   English   中英

如何使用Java使用Selenium WebDriver驗證動態庫?

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

問題:

畫廊中的圖像始終是動態的,但文章一直在變化。 當我單擊該文章時,圖像庫中會打開一個大圖像,而其他縮略圖會顯示一個輪播。


第一種情況:

我需要驗證圖像是否已加載並且圖庫是否正常 另外,其他縮略圖圖像也必須導航和驗證。

例:

    1. 我在文章頁面上,並且打開了該文章,因此該文章頁面上有一個圖庫,其中包含> 5張縮略圖圖像。
    1. 現在,我需要驗證圖像是否已打開,並且還要一個接一個地導航以驗證縮略圖。

第二種情況:

有時本文會加載圖像,有時會包含視頻或僅包含一個圖像。 所以這就是我要成為動態的意思。

所以每次圖像更改時,都需要知道如何處理這種情況,下面是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>

到目前為止,我所做的只是:

    1. 打開URL。
    1. 單擊文章頁面。
    1. 現在打開該/任何文章。

我的第四步是在打開文章時驗證該圖像(帶有縮略圖)。 我無法執行第4步。 我現在沒有任何錯誤,直到第3步我都可以成功完成。

嗨,請像下面這樣

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 
        }
    }

}

}

請隨時詢問是否有疑問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM