2013-03-16 67 views
0

我是新来的硒。其实我正在处理一些cookie验证项目,这需要我手动检查在多个浏览器(Firefox,即chrome,safari)中点击某些许可链接之前和之后存在的cookie。使用硒进行饼干验证

之前在第一阶段的项目中,我运行了一个qtp脚本,将firefox作为一个窗口对象并截取屏幕截图,但如果分辨率发生变化或出现任何次要的感觉变化,那就非常麻烦。此外它很难管理,它只能在Firefox上运行,我需要再次为Chrome和Safari浏览器编写相同的脚本。除此之外,由于QTP是许可产品,目前我们正在使用座位许可证,因此我无法在多台机器上运行它以加速执行。

所以我想转向硒。截至目前,我的要求是:

1. open the page - take the screenshot once page loaded. 
2. check the cookies using firebug or any other way - take the screenshot 
3. click the link to close the consent - take screenshot once consent closed. 
4. refresh the page and again check the cookies using firebug - take screenshot 

所以我做了硒一些研究,发现我可以使用verifyCookie验证Cookie,但仍然需要我饼干萤火虫窗口的屏幕截图。所以我被困在这里。

请帮我在这里..

我发现了一些可能的方式做到这一点在Firefox,但现在我期待着如果可能类似于Chrome浏览器的东西。谢谢

+0

其实这种很奇怪,但我想如果可以为铬准备类似的代码类型,因为没有我在Firefox中使用的那种可用于Chrome的萤火虫..谢谢 – codeomnitrix 2013-04-13 14:06:41

回答

0

得到了一些解决方案

public class Selenium1st { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException, AWTException{ 
     // TODO Auto-generated method stub  
     System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe"); 
     FirefoxProfile firefoxProfile = new FirefoxProfile(); 

     String domain = "extensions.firebug."; 
     firefoxProfile.setPreference("app.update.enabled", false); 
     firefoxProfile.addExtension(new File("E:\\softs\\selenium-2.29.0\\firebug\\firebug-1.11.2-fx.xpi")); 
     firefoxProfile.setPreference(domain + "currentVersion", "1.11.2"); 
     firefoxProfile.setPreference("extensions.firebug.cookies.enableSites", true); 
     firefoxProfile.setPreference("extensions.firebug.allPagesActivation", "on"); 

     firefoxProfile.setPreference(domain + "framePosition", "bottom"); 
     firefoxProfile.setPreference(domain + "defaultPanelName", "cookies"); 

     WebDriver driver = new FirefoxDriver(firefoxProfile); 
     driver.get("http://www.google.com/webhp?complete=1&hl=en"); 
     WebElement query = driver.findElement(By.name("q")); 
     query.sendKeys("Cheese"); 
     query.sendKeys("\n"); 
     Robot robot = new Robot(); 
     BufferedImage img = robot.createScreenCapture(new Rectangle(new Dimension(1024, 768))); 

     File path = new File("E:\\abc");//Path to your file 
     if(path.getName().indexOf(".jpg") == -1){ 
      path = new File(path.getPath() + ".jpg"); 
     }  
     ImageIO.write(img, "jpg", path);     
    } 

} 

可能是有用的。

0

Selenium无法以您想要的方式与Firefox扩展或浏览器进行交互。

你可以做的是做收集网页上的cookie的列表:

driver.manage().getCookies() 

这将给您对硒可见的所有Cookie的列表。请注意,这是相同的是在JavaScript控制台(并非所有的cookies通过JavaScript是可见的,例如饼干HTTPOnly属性设置)利用可见光饼干:

document.cookie 

我会建议你使用的getCookies ()以编程方式验证Cookie。

+0

其实Ardesco,cookie验证不是问题,实际上问题是捕获所有加载的cookie的屏幕截图。其实我需要将这些截图上传到质量中心。 – codeomnitrix 2013-03-19 04:07:53

+0

Selenium无法打开萤火虫并截取cookie面板。我想我一直在努力去理解为什么它有*是一个截图。为什么不让自动测试打印出已知的cookies列表并上传到质量中心足够好? – Ardesco 2013-03-19 06:35:49

+0

好吧..其实截图是必要的,以便它在qc中上传。无论如何,谢谢。 – codeomnitrix 2013-03-19 06:54:55

0

在硒IDE如果你想利用页面的截图使用captureEntirePageScreenshot命令

captureEntirePageScreenshot | D:\\test.png | 

    D:\\test.png - path of file where you want to save the file