【问题标题】:How to verify `onclick` event in javascript for an Anchor tag using selenium webdriver如何使用 selenium webdriver 在 javascript 中验证 Anchor 标记的“onclick”事件
【发布时间】:2015-08-04 16:19:28
【问题描述】:

如何在下面的html中验证与锚标记关联的onclick事件:

<div class="itp-sb-left" style="float:left; width:100%; border:solid 1px #cccccc; padding:2px 0 2px 5px;">
            <a title="Submit to Facebook" target="_parent"  
             onclick="window.open('https://www.facebook.com/sharer.php? 
              s=100&p[title]=  
                 Singh&p[summary]=%27ll+try+to+describe+some+of+the+ideas+here.+Matrix+factorization+techniques+and+ensemble+methods+are+perhaps+the+algorithms+most+often+discussed+in+connection+with+the+Netflix+Prize%2C+but+a+lot+of+othe...&p[url]=http%3A%2F%2Fwww.jobma.com%2F1946&&p[images][0]=http%3A%2F%2Fwww.jobma.com%2Fpitcher%2FPhoto%2Fthumb_profile-1432116605.jpg', 'sharer', 'toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">


<img alt="Submit to Facebook" src="http://www.jobma.com/images/facebook.png"/>
</a>

【问题讨论】:

标签: html testing selenium-webdriver automation


【解决方案1】:

据我了解,您希望验证以下 onclick JS 事件:

window.open('https://www.facebook.com/sharer.php? 
              s=100&p[title]=  
                 Singh&p[summary]=%27ll+try+to+describe+some+of+the+ideas+here.+Matrix+factorization+techniques+and+ensemble+methods+are+perhaps+the+algorithms+most+often+discussed+in+connection+with+the+Netflix+Prize%2C+but+a+lot+of+othe...&p[url]=http%3A%2F%2Fwww.jobma.com%2F1946&&p[images][0]=http%3A%2F%2Fwww.jobma.com%2Fpitcher%2FPhoto%2Fthumb_profile-1432116605.jpg', 'sharer', 'toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">

由于这是在新窗口中打开的,因此您可以验证 URL 在新窗口中的打开情况,如下所示:

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());

driver.switchTo().window(tabs.get(0));
String urlInNewTab = driver.getCurrentUrl();
Assert.assertEquals(urlInNewTab, 'YOUR_EXPECTED_URL_HERE);

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2014-11-18
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多