qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          selenium技巧——通過js來控制滾動條

           業(yè)務(wù)流程:
            1.打開此網(wǎng)頁 http://nanjing.xiaomishu.com/shop/search/sp2048_745
            2.向下拖動滾動條,右下角自動會出現(xiàn)【投訴與建議】(此網(wǎng)頁已經(jīng)修改不拖動也出現(xiàn)了,以前是沒有的,)
            3.點擊【投訴與建議】
            4.在打開的div 層中輸入姓名,郵件,內(nèi)容 并點擊確定
            5.驗證頁面上的提示文字
           



          package com.example.tests;

          import org.junit.*;
          import org.openqa.selenium.*;
          import org.openqa.selenium.ie.InternetExplorerDriver;


          public class SeleniumWebDriver {
           public static WebDriver driver;
           
           @Test
           public void testUnit() {
            driver = new InternetExplorerDriver();
            driver.get(http://nanjing.xiaomishu.com/shop/search/sp2048_745);
            
            maxBrowser(driver);
            setScroll(driver,500);

            driver.findElement(By.linkText("投訴與建議")).click(); 
            driver.findElement(By.xpath("http://input[@id='repName']")).sendKeys("1");
            driver.findElement(By.xpath("http://input[@id='repMail']")).sendKeys("1");
            driver.findElement(By.xpath("http://textarea[@id='repContent']")).sendKeys("hello");
            driver.findElement(By.xpath("http://a[@id='repBtn']")).click();
            
            Assert.assertEquals("您輸入的郵箱格式不正確", driver.findElement(By.xpath("http://div[@id='floatBox_remind']/span")).getText());
            
           }
           
           
           //將IE最大化
            public static void  maxBrowser(WebDriver driver){
             try {
              String maxBroswer = "if (window.screen) {window.moveTo(0, 0);" +
                "window.resizeTo(window.screen.availWidth,window.screen.availHeight);}";
              
              JavascriptExecutor jse=(JavascriptExecutor) driver;
              jse.executeScript(maxBroswer);
             } catch (Exception e) {
              System.out.println("Fail to  Maximization browser");
             }
            }
           
           //將滾動條滾到適合的位置
           public static void setScroll(WebDriver driver,int height){
            try {
             String setscroll = "document.documentElement.scrollTop=" + height;
             
             JavascriptExecutor jse=(JavascriptExecutor) driver;
             jse.executeScript(setscroll);
            } catch (Exception e) {
             System.out.println("Fail to set the scroll.");
            }   
           } 
          }

            對于這種頂級div層,一開始用id來定位,在firefox中可以正常跑

            在IE中會報stack overflow的錯誤,一直以來是以為沒有加等待時間而沒找到

            后來試了很多方法,最后發(fā)現(xiàn)用xpath就解決了,真是耽誤了很久的時間

            給大家借鑒,如果一個定位方法不能用時,多換換其他的

            這個例子中我們學(xué)習(xí)了如何用JS控制滾動條,如何最大化IE頁面。

          posted on 2013-09-13 11:32 順其自然EVO 閱讀(1068) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學(xué)習(xí)

          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 板桥市| 茶陵县| 安徽省| 宿松县| 黔西| 西丰县| 澎湖县| 怀安县| 武汉市| 徐水县| 济宁市| 马公市| 奉贤区| 左贡县| 肇州县| 北碚区| 夏河县| 新建县| 汶上县| 华阴市| 灵丘县| 靖远县| 房产| 南昌市| 屯留县| 托克逊县| 丰镇市| 凌源市| 德保县| 绵竹市| 建阳市| 韩城市| 连江县| 辽阳市| 合阳县| 阜城县| 桦南县| 子长县| 乌苏市| 五原县| 逊克县|