java 执行js selenium_如何在Selenium WebDriver Java中使用JavaScript

带有Selenium WebDriver的JavaScript

Selenium是最受欢迎的自动化测试套件之一。Selenium的设计方式旨在支持和鼓励对基于Web的应用程序以及各种浏览器和平台的功能方面进行自动化测试。

public static WebDriver driver;

public static void main(String[] args) {

driver = new FirefoxDriver(); // This opens a window

String url = "----";

/*driver.findElement(By.id("username")).sendKeys("yashwanth.m");

driver.findElement(By.name("j_password")).sendKeys("yashwanth@123");*/

JavascriptExecutor jse = (JavascriptExecutor) driver;

if (jse instanceof WebDriver) {

//Launching the browser application

jse.executeScript("window.location = \'"+url+"\'");

jse.executeScript("document.getElementById('username').value = \"yash\";");

// Tag having name then

driver.findElement(By.xpath(".//input[@name='j_password']")).sendKeys("admin");

//Opend Site and click on some links. then you can apply go(-1)--> back  forword(-1)--> front.

//Refresheing the web-site. driver.navigate().refresh();

jse.executeScript("window.history.go(0)");

jse.executeScript("window.history.go(-2)");

jse.executeScript("window.history.forward(-2)");

String title = (String)jse.executeScript("return document.title");

System.out.println(" Title Of site : "+title);

String domain = (String)jse.executeScript("return document.domain");

System.out.println("Web Site Domain-Name : "+domain);

// To get all NodeList[1052] document.querySelectorAll('*');  or document.all

jse.executeAsyncScript("document.getElementsByTagName('*')");

String error=(String) jse.executeScript("return window.jsErrors");

System.out.println("Windowerrors  :   "+error);

System.out.println("To Find the input tag position from top");

ArrayList> al =  (ArrayList>) jse.executeScript(

"var source = [];"+

"var inputs = document.getElementsByTagName('input');"+

"for(var i = 0; i < inputs.length; i++) { " +

"   source[i] = inputs[i].offsetParent.offsetTop" +      //"    inputs[i].type = 'radio';"

"}"+

"return source"

);//inputs[i].offsetParent.offsetTop     inputs[i].type

System.out.println("next");

System.out.println("array : "+al);

// (CTRL + a) to access keyboard keys. org.openqa.selenium.Keys

Keys k = null;

String selectAll = Keys.chord(Keys.CONTROL, "a");

WebElement body = driver.findElement(By.tagName("body"));

body.sendKeys(selectAll);

// Search for text in Site. Gets all ViewSource content and checks their.

if (driver.getPageSource().contains("login")) {

System.out.println("Text present in Web Site");

}

Long clent_height = (Long) jse.executeScript("return document.body.clientHeight");

System.out.println("Client Body Height : "+clent_height);

// using selenium we con only execute script but not JS-functions.

}

driver.quit(); // to close browser

}

要执行用户功能,请将JS写入文件并读取为String并执行以方便使用。

Scanner sc = new Scanner(new FileInputStream(new File("JsFile.txt")));

String js_TxtFile = "";

while (sc.hasNext()) {

String[] s = sc.next().split("\r\n");

for (int i = 0; i < s.length; i++) {

js_TxtFile += s[i];

js_TxtFile += " ";

}

}

String title =  (String) jse.executeScript(js_TxtFile);

System.out.println("Title  : "+title);

document.title和document.getElementById()是浏览器中可用的属性/方法。

JsFile.txt

var title = getTitle();

return title;

function getTitle() {

return document.title;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值