qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Selenium下載百度音樂并驗證

          package baidu;
          import java.io.File;
          import java.io.IOException;
          import java.util.List;
          import org.apache.commons.io.FileUtils;
          import org.openqa.selenium.By;
          import org.openqa.selenium.Keys;
          import org.openqa.selenium.OutputType;
          import org.openqa.selenium.TakesScreenshot;
          import org.openqa.selenium.WebDriver;
          //import org.openqa.selenium.WebDriver.Navigation;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.chrome.ChromeDriver;
          import org.openqa.selenium.interactions.Actions;
          public class selenium  {
          public static void snapshot(TakesScreenshot drivername, String filename)
          {
          // this method will take screen shot ,require two parameters ,one is driver name, another is file name
          File scrFile = drivername.getScreenshotAs(OutputType.FILE);
          // Now you can do whatever you need to do with it, for example copy somewhere
          try {
          System.out.println("save snapshot path is:E:/"+filename);
          FileUtils.copyFile(scrFile, new File("E:\\"+filename));
          } catch (IOException e) {
          // TODO Auto-generated catch block
          System.out.println("Can't save screenshot");
          e.printStackTrace();
          }
          finally
          {
          System.out.println("screen shot finished");
          }
          }
          public static void main (String [] args) throws InterruptedException
          {
          String URL="http://61.135.169.105/";
          //avoid Chrome warnning message like "unsupported command-line flag --ignore-certificate-errors. "
          ChromeOptions options = new ChromeOptions();
          options.addArguments("--test-type");
          System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
          WebDriver driver = new ChromeDriver(options);
          driver.get(URL);
          //max size the browser
          driver.manage().window().maximize();
          /*
          Navigation navigation = driver.navigate();
          navigation.to(URL);*/
          Thread.sleep(2000);
          snapshot((TakesScreenshot)driver,"open_baidu.png");
          //WebElement reg=driver.findElement(By.name("tj_reg"));
          //reg.click();
          //    WebElement keyWord = driver.findElement(By.id("kw1"));
          //find the element
          WebElement keyWord = driver.findElement(By.xpath("http://input[@id='kw1']"));
          keyWord.clear();
          //send key words
          keyWord.sendKeys("小蘋果");
          Thread.sleep(3000);
          snapshot((TakesScreenshot)driver,"input_keyWord.png");
          WebElement submit = driver.findElement(By.id("su1"));
          System.out.println(submit.getLocation());
          submit.click();
          //System.out.println(driver.getWindowHandle());
          Thread.sleep(5000);
          WebElement se=driver.findElement(By.xpath("http://*[@id=\"2\"]/div[1]/div[2]/table/tbody/tr/td[5]/span/a")) ;
          Actions action = new Actions(driver);
          action.clickAndHold(se);
          action.sendKeys(Keys.DOWN);
          Thread.sleep(5000);
          List<WebElement> elementList = driver.findElements(By.tagName("herf"));
          for(WebElement e:elementList)
          {
          System.out.print("-->"+e.getText());
          }
          //se.click();
          // System.out.println(driver.getPageSource());
          //  System.out.println(pageSource);
          //WebElement link =driver.findElement(By.xpath(SELENIUM_LINK));
          WebElement link =driver.findElement(By.xpath("http://*[@id=\"2\"]/div[1]/div[2]/table/tbody/tr/td[5]/span/a"));//By.xpath("http://*[@id=\"1\"]/h3/a"));     //*[@id="1"]/h3/a
          link.click();
          Thread.sleep(5000);
          driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]);
          Thread.sleep(5000);
          WebElement down =driver.findElement(By.xpath("http://*[@id=\"128\"]"));//<i class="icon btn-icon-download-small"></i>
          Thread.sleep(5000);
          down.click();
          snapshot((TakesScreenshot)driver,"down_m.png");
          //get page title
          System.out.println(driver.getTitle());
          Thread.sleep(5000);
          WebElement userName=driver.findElement(By.id("TANGRAM__PSP_8__userName"));
          WebElement password=driver.findElement(By.id("TANGRAM__PSP_8__password"));
          WebElement login=driver.findElement(By.id("TANGRAM__PSP_8__submit"));
          Thread.sleep(5000);
          userName.sendKeys("QAtest");//your baidu userName
          password.sendKeys("mypassword");//your baidu password
          login.submit();
          //     navigation.back();
          snapshot((TakesScreenshot)driver,"open_bake.png");
          System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
          Thread.sleep(50000);
          File file=new File("C:\\Users\\Young\\Downloads\\小蘋果.mp3");
          if(file.exists())
          {
          System.out.println("PASS");
          }
          else
          {
          System.out.println("FAIL");
          }
          driver.quit();
          }
          }
            如果出現:unsupported command-line flag --ignore-certificate-errors.
            //avoid Chrome warnning message like "unsupported command-line flag --ignore-certificate-errors. "
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--test-type");
            System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
            WebDriver driver = new ChromeDriver(options);
          English »
           

          posted on 2014-09-18 09:28 順其自然EVO 閱讀(334) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄selenium and watir webdrivers 自動化測試學習

          <2014年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 保山市| 永胜县| 莱芜市| 祥云县| 上饶县| 安吉县| 岳池县| 新干县| 安宁市| 云南省| 曲周县| 来宾市| 巴彦县| 九寨沟县| 宕昌县| 西城区| 托克逊县| 白山市| 文水县| 同心县| 吴堡县| 灵山县| 利辛县| 乌恰县| 潮安县| 依兰县| 万宁市| 阿拉善左旗| 保德县| 宣威市| 石家庄市| 湄潭县| 珲春市| 绿春县| 乌拉特后旗| 栾城县| 安福县| 湘潭县| 石渠县| 营口市| 山西省|