2017-03-20 51 views
1

,而試圖調整與硒的Java代碼手風琴的演示網址http://www.globalsqa.com/demo-site/accordion-and-tabs/調整大小手風琴與硒的Junit

重新大小手風琴標籤

給定的HTML代碼是

<body> 
<h3 class="docs">Resize the outer container:</h3> 
<div id="accordion-resizer" class="ui-widget-content ui-resizable"> 
<div id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist"> 
<h3 class="ui-accordion-header ui-corner-top ui-state-default ui-accordion-header-active ui-state-active ui-accordion-icons" role="tab" id="ui-id-1" aria-controls="ui-id-2" aria-selected="true" aria-expanded="true" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>Section 1</h3> 
<div class="ui-accordion-content ui-corner-bottom ui-helper-reset ui-widget-content ui-accordion-content-active" id="ui-id-2" aria-labelledby="ui-id-1" role="tabpanel" aria-hidden="false" style="display: block; height: 28px; overflow: auto;"> 
<p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p> 
</div> 
<h3 class="ui-accordion-header ui-corner-top ui-accordion-header-collapsed ui-corner-all ui-state-default ui-accordion-icons" role="tab" id="ui-id-3" aria-controls="ui-id-4" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>Section 2</h3> 
<div class="ui-accordion-content ui-corner-bottom ui-helper-reset ui-widget-content" id="ui-id-4" aria-labelledby="ui-id-3" role="tabpanel" aria-hidden="true" style="display: none; height: 28px; overflow: auto;"> 
<p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p> 
</div> 
<h3 class="ui-accordion-header ui-corner-top ui-accordion-header-collapsed ui-corner-all ui-state-default ui-accordion-icons" role="tab" id="ui-id-5" aria-controls="ui-id-6" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>Section 3</h3> 
<div class="ui-accordion-content ui-corner-bottom ui-helper-reset ui-widget-content" id="ui-id-6" aria-labelledby="ui-id-5" role="tabpanel" aria-hidden="true" style="display: none; height: 28px; overflow: auto;"> 
<p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p> 
<ul> 
<li>List item one</li> 
<li>List item two</li> 
<li>List item three</li> 
</ul> 
</div> 
<h3 class="ui-accordion-header ui-corner-top ui-accordion-header-collapsed ui-corner-all ui-state-default ui-accordion-icons" role="tab" id="ui-id-7" aria-controls="ui-id-8" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>Section 4</h3> 
<div class="ui-accordion-content ui-corner-bottom ui-helper-reset ui-widget-content" id="ui-id-8" aria-labelledby="ui-id-7" role="tabpanel" aria-hidden="true" style="display: none; height: 28px; overflow: auto;"> 
<p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p><p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p> 
</div> 
</div> 
<div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div><div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div></div> 
<div class="demo-description"> 
</div> 


</body> 

以下給出用於調整元素大小的Java代碼

@Test 
public void test() throws InterruptedException { 
    driver.manage().window().maximize(); 
    driver.get("http://www.globalsqa.com/demo-site/accordion-and-tabs/"); 

    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 

    WebElement tab1 = driver.findElement(By.xpath("//*[@id='Simple Accordion']")); 
    WebElement tab2 = driver.findElement(By.xpath("//*[@id='Re-Size Accordion']")); 
    WebElement tab3 = driver.findElement(By.xpath("//*[@id='Toggle Icons']")); 

    tab1.click(); 
    String var = tab1.getAttribute("class"); 
    System.out.println("The Class Attribute for First Tab:: " + var); 
    Thread.sleep(3000); 

    WebElement lists = driver.findElement(By.xpath("//*[@id='post-2654']/div[2]/div[1]/div/div[1]/p/iframe")); 
    lists.click(); 
    driver.switchTo().frame(driver.findElement(By.cssSelector(".demo-frame"))); 
    List<WebElement> expandableLists = driver.findElements(By.cssSelector(".ui-accordion-header")); 
    System.out.println("Lists Size:: "+expandableLists.size()); 

    WebElement element = expandableLists.get(2); 
    new Actions(driver).click(element).build().perform(); 

    //WebElement sectionContent = driver.findElement(By.xpath("//*[@id='ui-id-6']/p")); 
    WebElement sectionContent = driver.findElement(By.cssSelector(".ui-accordion-content[id = ui-id-6]")); 
    //WebElement sectionContent = driver.findElement(By.xpath("html/body/div[1]/div[3]/p")); 

    Thread.sleep(2000); 
    String ssTxt = sectionContent.findElement(By.tagName("p")).getText(); 
    //String s[] = ssTxt.split(","); 
    System.out.println("The Content is:: "+ssTxt); 

    /* 
    for (int i = 0; i < s.length; i++) { 
     System.out.println("Strings after Split---> "+ s[i]); 
    } 
    */ 
    String ssTxt1 = sectionContent.findElement(By.tagName("ul")).getText(); 
    System.out.println("The Points of the Descr is: "+ssTxt1); 
    String ss[] = ssTxt1.split("\n"); 
    for (int i = 0; i < ss.length; i++) { 
     System.out.println("After Splitting ss: "+ss[i]); 
    } 
    driver.switchTo().defaultContent(); 
    Thread.sleep(1000); 

    JavascriptExecutor jse = (JavascriptExecutor) driver; 
    jse.executeScript("window.scrollBy(0,-100)", ""); 
    tab2.click(); 

    WebElement vContainer = driver.findElement(By.xpath("//*[@id='post-2654']/div[2]/div[1]/div/div[2]/p")); 
    //WebElement iFrame = driver.findElement(By.cssSelector(".demo-frame")); 

    String chck = vContainer.findElement(By.tagName("iframe")).getAttribute("src").toString(); 
    System.out.println("Just to Check the Source: "+chck); 

    WebElement iFrame = vContainer.findElement(By.tagName("iframe")); 

    String s1 = iFrame.getAttribute("width").toString(); 
    System.out.println("Just to check inside IFrame: "+s1); 

    driver.switchTo().frame(iFrame); 
    WebElement accordionResizer = driver.findElement(By.xpath("//*[@id='accordion-resizer']")); 
    String s2 = accordionResizer.getAttribute("id").toString(); 
    //String s2 = iFrame.findElement(By.tagName("div")).getAttribute("id").toString(); 
    System.out.println("Check for Accordion: "+s2); 

    String sSize = accordionResizer.getAttribute("style").toString(); 
    System.out.println("The Size of the Element is (accordionResizer):: "+ sSize);  
    WebElement accordianContainer = driver.findElement(By.xpath("//*[@id='accordion']")); 
    String s3 = accordianContainer.getAttribute("class").toString(); 
    System.out.println("The Container accordian: "+s3); 

    //driver.switchTo().frame(accordionResizer); 
    //Creating a Web element for the Accordion resizable handler 
    WebElement resizableElement = driver.findElement(By.xpath("//*[@id='accordion-resizer']/div[4]")); 
    String s4 = resizableElement.getAttribute("class").toString(); 
    System.out.println("The Resizable Handler: "+s4); 

    int x = resizableElement.getSize().height; 
    int y = resizableElement.getSize().width; 

    System.out.println("The Size of the Frame:: "+ x + " The Width: "+ y); 

    //WebElement eWidthResizable = driver.findElement(By.xpath("//*[@id='accordion-resizer']/div[3]")); 

    Actions act = new Actions(driver); 

    //act.moveToElement(resizableElement).dragAndDropBy(resizableElement, 400,400).build().perform(); 
    act.moveToElement(resizableElement); 
    act.clickAndHold(); 
    act.moveByOffset(400, 400); 
    act.release(); 
    act.perform(); 

    driver.switchTo().defaultContent(); 
    System.out.println("End of All"); 
} 

雖然可重新調整的元素被點擊並保留代碼,但問題在於腳本停止後光標仍在移動,並且由於手動移動光標,手風琴仍在調整大小。

請別人幫助我,讓手風琴調整大小,並在此之後釋放光標。

回答

1

你必須明白,手風琴控制被放置在不同的幀所以首先我們要轉到該幀:

System.setProperty("webdriver.chrome.driver", "C:\\Eclipse\\Selenium\\chromedriver.exe"); 

    WebDriver driver = new ChromeDriver(); 

    driver.manage().window().maximize(); 


    driver.get("http://www.globalsqa.com/demo-site/accordion-and-tabs/#Re-Size Accordion"); 

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

    Thread.sleep(10000); 


/* //Goto header frame 

    WebElement headerFrame = driver.findElement(By.xpath("/html/body/div[1]/iframe")); 

    driver.switchTo().frame(headerFrame); 




    //click close icon on header frame 

    driver.findElement(By.xpath("//*[@id='close-icon']")).click(); 

    //Switch back to initial frame 
    driver.switchTo().defaultContent(); 
    */ 


    //Switch to new frame containing Accordion controls 
    WebElement myFrame = driver.findElement(By.cssSelector("#post-2654 > div.twelve.columns > div.newtabs.horizontal > div > div.single_tab_div.resp-tab-content.resp-tab-content-active > p:nth-child(2) > iframe")); 

    driver.switchTo().frame(myFrame); 

    Actions action = new Actions(driver); 


    //element to be streched 
    WebElement from = driver.findElement(By.cssSelector("div#accordion+div")); 


    System.out.println(from.getText()); 

    Thread.sleep(10000); 
    action.clickAndHold(from).moveByOffset(50,10).release(from).build().perform(); 

    JavascriptExecutor js = (JavascriptExecutor) driver; 

    js.executeScript("document.getElementById('accordion-resizer').setAttribute('style', 'width: 700px; height: 400px;')"); 

評的代碼塊,如果你看到的是需要「元素不會在這個點擊時間「的例外。