selenium 右键下载图片,结合sikuli

上一次写右键下载是结合robot,这次是使用selenium+sikuli

上一次日志:http://www.cnblogs.com/tobecrazy/p/3969390.html

有关sikuli的介绍,和简单使用请参考:http://www.cnblogs.com/tobecrazy/p/4516369.html

关于sikuli的缺点:

  1.运行脚本时候,必须截图,图片比较占用系统空间

     2.脚本执行过程中,不能移动鼠标,而selenium可以最小化,任意移动鼠标

话不多说,介绍一下场景:

  1.打开百度

  2.右键单击百度logo

  3.点击弹出菜单的save as

  4. 输入保存位置和文件名

  5. 点击保存菜单

首先要做的是,截图,截图保存位置c:/selenium

       

脚本如下:

   //create chrome webDriver with parameter
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");
       
        Screen screen=new Screen();
        System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");  
        WebDriver driver = new ChromeDriver(options);
 
        String login_url="https://www.baidu.com/";
        driver.get(login_url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
        
        //find baidu logo 
        WebElement baiduLogo=driver.findElement(By.xpath("//div[@id='lg']/img"));
        Actions actions = new Actions(driver);
        //right click
        actions.contextClick(baiduLogo).perform();
        Pattern saveAs =new Pattern("c:\\selenium\\saveAs.png");
        screen.click(saveAs);
        Pattern inputBox1 =new Pattern("c:\\selenium\\inputBox1.png");
        screen.find(inputBox1);
        screen.type("c:\\selenium\\baiduLogo.png");
        Pattern saveButton =new Pattern("c:\\selenium\\saveButton.png");
        screen.click(saveButton);
        //wait for 60s to verify download success or not 
        try
        {
            Thread.sleep(60000);
            
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            File f=new File("c:\\selenium\\baiduLogo.png");
            if(f.exists())
            {
                System.out.println("PASS");
            }
            driver.close();
        }
        

 

posted @ 2015-05-22 21:18  to be crazy  阅读(3489)  评论(0编辑  收藏  举报