Javascript 使用casperjs模拟键盘动作

Javascript 使用casperjs模拟键盘动作,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,如何使用casperjs模拟键盘操作/事件?。我在按键盘操作时卡住了,比如shift+alt+enter,ctrl+}和ctrl+shift+>等等。。 有人能帮我做这种事吗 用下列方法进行试验 this.sendKeys('div.edit-code > textarea:nth-child(1)', 'enter', {modifiers: 'shift + alt'}); 编辑: 我需要使用的键盘快捷键执行单元格,场景如下: 使用'+'符号创建新单元格 向单元格中添加一些内容 现

如何使用casperjs模拟键盘操作/事件?。我在按键盘操作时卡住了,比如
shift+alt+enter
ctrl+}
ctrl+shift+>
等等。。 有人能帮我做这种事吗

用下列方法进行试验

this.sendKeys('div.edit-code > textarea:nth-child(1)', 'enter', {modifiers: 'shift + alt'});
编辑: 我需要使用的键盘快捷键执行单元格,场景如下:

  • 使用
    '+'
    符号创建新单元格
  • 向单元格中添加一些内容
  • 现在使用键盘快捷键,需要执行(例如:
    “shift+alt+enter”
    单元格

    • 我编写了一个小测试用例:

      keyboard.html(捕获键盘事件并将其写入div):

      输出

      var casper = require('casper').create();
      
      casper.start('http://localhost:63344/CasperSheet/keyboard.html');
      
      function testKey(key, modifiers) {
          casper.then(function () {
              casper.sendKeys("#text", key, {modifiers: modifiers});
          }).then(function () {
              casper.echo(casper.evaluate(function () {
                  return document.querySelector("#text").textContent
              }))
          })
      }
      
      testKey('a');
      testKey('}', 'ctrl')
      testKey('>', 'ctrl+alt')
      testKey('\n', 'shift+alt')
      casper.run();
      
      a
      CTRL + }
      CTRL + ALT + >
      ALT + SHIFT + 
      //a new line here...
      

      来看看你的代码,我的建议:

    • 'enter'
      应该是
      '\n'
      。如果使用
      'enter'
      ,它将发送
      'e'
      'n'
      't'
      'e'
      'r'
      五个键盘事件
    • {modifiers:'shift+alt'}
      应该是
      {modifiers:'shift+alt'}
      。因为modifiers组合的实现不会修剪空间…您可以向CasperJS发布一个pull请求来修复这个问题

    • 好的,让我们讨论一下如何在该web应用程序中运行代码…我发现我们可以通过单击
      play
      按钮来运行代码,因此不需要通过
      alt+shift+enter来运行代码,这很难实现

      这个剧本适合我:

      var casper = require('casper').create({
          verbose: true,
          logLevel: 'debug',
          viewportSize: {
              width: 1600,
              height: 900
          }
      });
      
      var cookie = "user=Sayalic0; token=<some_token>";
      var domain = "rcloud.social";
      cookie.split(";").forEach(function(pair){
          pair = pair.split("=");
          phantom.addCookie({
              'name': pair[0],
              'value': pair[1],
              'domain': domain
          });
      });
      
      
      casper.start('https://rcloud.social/edit.html?notebook=<note_book_id>')
      
      casper.then(function () {
          casper.wait(30000);//wait for page load
      }).then(function () {
          casper.capture('1.png')
      }).then(function () {
          casper.click("#run-notebook > i")// click run
      }).then(function () {
          casper.wait(10000)//wait code running ends
      }).then(function () {
          casper.capture('2.png');
      })
      
      casper.run()
      
      var casper=require('casper')。创建({
      没错,
      日志级别:“调试”,
      视口大小:{
      宽度:1600,
      身高:900
      }
      });
      var cookie=“user=Sayalic0;token=”;
      var domain=“rcloud.social”;
      cookie.split(“;”).forEach(函数(对){
      pair=pair.split(“=”);
      幻影·艾德库奇({
      “名称”:对[0],
      “值”:对[1],
      “域”:域
      });
      });
      卡斯珀,开始https://rcloud.social/edit.html?notebook=')
      casper.then(函数(){
      casper.wait(30000);//等待页面加载
      }).然后(函数(){
      casper.capture('1.png')
      }).然后(函数(){
      casper.click(“#run notebook>i”)//单击run
      }).然后(函数(){
      wait(10000)//等待代码运行结束
      }).然后(函数(){
      casper.capture('2.png');
      })
      casper.run()
      
      屏幕截图

      var casper = require('casper').create();
      
      casper.start('http://localhost:63344/CasperSheet/keyboard.html');
      
      function testKey(key, modifiers) {
          casper.then(function () {
              casper.sendKeys("#text", key, {modifiers: modifiers});
          }).then(function () {
              casper.echo(casper.evaluate(function () {
                  return document.querySelector("#text").textContent
              }))
          })
      }
      
      testKey('a');
      testKey('}', 'ctrl')
      testKey('>', 'ctrl+alt')
      testKey('\n', 'shift+alt')
      casper.run();
      
      a
      CTRL + }
      CTRL + ALT + >
      ALT + SHIFT + 
      //a new line here...
      
      1.png

      2.png