我一直试图找到一种方法来确认光标位于输入字段内(模拟在该字段内点击)。
如果字段条目验证失败,则会在网页顶部调用错误消息。
错误消息是一个超链接,当按下该按钮时,滚动指向该输入字段所在的页面,并将光标放在该字段中。
有没有办法确认光标在框中?
由于
答案 0 :(得分:2)
当光标位于文本字段中时,表示文本字段处于焦点或是活动元素。 Capybara没有直接为此提供方法。但是,有几种选择。
切换到有效元素
如果您下拉到底层的Selenium驱动程序,则可以检索活动元素:
piotrek@piotrek-Inspiron-5423:~/test$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 6.2.0-5ubuntu12' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12)
piotrek@piotrek-Inspiron-5423:~/test$ clang++ -v
clang version 3.8.1-12ubuntu1 (tags/RELEASE_381/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.2.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
piotrek@piotrek-Inspiron-5423:~/test$ cat test.cpp
#include <tuple>
std::tuple<int, int> f()
{
return {1,1};
}
piotrek@piotrek-Inspiron-5423:~/test$ clang++ -Wall -pedantic -fsyntax-only -std=c++14 test.cpp
piotrek@piotrek-Inspiron-5423:~/test$ clang++ -Wall -pedantic -fsyntax-only -std=c++11 test.cpp
piotrek@piotrek-Inspiron-5423:~/test$ g++ -Wall -pedantic -fsyntax-only -std=c++14 test.cpp
piotrek@piotrek-Inspiron-5423:~/test$ g++ -Wall -pedantic -fsyntax-only -std=c++11 test.cpp
piotrek@piotrek-Inspiron-5423:~/test$
使用活动元素,您可以检查它是否是您期望的元素:
string pdfFolder;
#if DEBUG
DirectoryInfo directoryInfo = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName);
pdfFolder = directoryInfo.FullName + @"\PdfFile\Auto Refill Reminder List.pdf";
System.Diagnostics.Process.Start(pdfFolder );
#endif
#if (!DEBUG)
pdfFolder = Environment.CurrentDirectory + @"\PdfFile\Auto Refill Reminder List.pdf";
Process.Start(pdfFolder);
#endif
使用page.driver.browser.switch_to.active_element
=> #<Selenium::WebDriver::Element:0x34409cfc id="0.6429182184051125-3">
另一种方法是使用JavaScript获取有关活动元素的一些细节。例如,以下内容返回焦点元素的id:
# Use Capybara to find the element you expect to be in focus
expected_element = page.find("#field_id")
# Get the element that is actually in focus
active_element = page.driver.browser.switch_to.active_element
# Check that the two elements are the same
# Note that you need to call `native` so that you are comparing Selenium elements
expect(active_element).to eq(expected_element)