我正在使用Protractor JS.该网站是用Angular JS编写的.
所以我有一个拨动开关.我注意到,当你关闭或打开时,切换开关的值从true变为false,false变为true.
我正在尝试在Protractor访问我的页面时创建一个条件,当它看到切换开关'off'时它将'打开'.如果拨动开关已经"打开",它将首先将其关闭,然后再将其打开.
我提出了这个代码,但由于某种原因它无法正常工作:
if( expect(element(By.id('toggle-switch')).element(By.css('[value="false"]')).isDisplayed()) ) {
element(By.id('toggle-switch')).click();
console.log('in the if')
}
else{
element(By.id('toggle-switch')).click();
browser.sleep(3000);
element(By.id('toggle-switch')).click();
console.log('in the else')
}
Run Code Online (Sandbox Code Playgroud)
此代码似乎仅适用于if语句.由于某种原因,它永远不会去其他地方.这是我收到的错误:
NoSuchElementError:找不到使用locator的元素:By.cssSelector("[value = \"false \"]")
所以我试过了
.isPresent()而不是.isDisplayed()
我不再收到上述错误,但由于某些原因,当使用.isPresent()时,它总是转到if语句并且只运行那个,而不是else语句.没有显示错误.
如果有更好的方法请告诉我.这似乎非常有限,因为无法在此框架中创建适当的条件.
我目前使用Eclipse作为我的Java IDE,我使用Maven.我单击运行按钮,它可以运行我写的Selenium Java测试.
然后我继续在我的本地机器上安装Maven.
转到我的pom.xml文件所在的目录后.我运行命令:mvn test
我收到以下结果:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBu
ilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SeleniumWebDriver 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumWebDriver ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platfo
rm dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ SeleniumWebDriver ---
[INFO] Nothing to compile - all classes are up to date …Run Code Online (Sandbox Code Playgroud) 所以我有以下html源代码:
<form action='http://example.com' method='get'>
<P>Some example text here.</P>
<input type='text' class='is-input' id='agent_name' name='deviceName' placeholder='Device Name'>
<input type='hidden' name='p' value='firefox'>
<input type='hidden' name='email' value='example@example.com'>
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
<p><input type='submit' class='btn-blue' style='margin-top:15px;' value='Install'></p>
</form>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个html源代码保存为字符串.我想用jsoup之类的东西来解析它.并获取以下字符串:
<input type='hidden' name='k' value='cITBk236gyd56oiY0fhk6lpuo9nt61Va'>
或者更好的是,只获取以下值: cITBk236gyd56oiY0fhk6lpuo9nt61Va
我遇到的问题是:
a)该值:cITBk236gyd56oiY0fhk6lpuo9nt61Va一直在变化我无法找到整个html标签.
所以,我正在寻找一种更好的方法来做到这一点.以下是我目前看来不起作用的内容:
//tried use thing, but java was angry for some reason
Jsoup.parse(myString);
// so I used this instead.
org.jsoup.nodes.Document doc = Jsoup.parse(myString);
// in this case I just tried to select the entire tag. Elements
elements …Run Code Online (Sandbox Code Playgroud) 所以我有一个 rspec 测试,我试图让我的程序进行验证:
来自 rspec 测试的代码片段:
context '#validate(test_tool)' do
it { expect(test_tool.validate).to raise_error StandardError }
Run Code Online (Sandbox Code Playgroud)
来自 test_tool 的代码片段(test_tool 是我为通过 rspec 测试而编写的程序) 在 test_tool 内部有多种方法,但我只是提取了 rspec 测试正在寻找的一种方法:
def self.validate
raise StandardError
end
Run Code Online (Sandbox Code Playgroud)
我到处谷歌搜索。无论我如何尝试引发此错误。我不断收到错误消息。请参阅控制台输出的片段:
example at ./spec/training_site/spec_test.rb:56 (FAILED - 1)
Failures:
1) MyClass #validate(test_tool)
Failure/Error: it { expect(MyClass.validate).to raise_error StandardError }
StandardError:
StandardError
Run Code Online (Sandbox Code Playgroud)
第 56 行只是第一个代码片段的第二行。
基本上,我正在寻找故障排除和/或有关如何完成此操作的示例。
我的C#程序运行一个进程,当单击特定按钮时,该进程启动一个命令提示符应用程序,并向其传递参数。
程序从用户选择中获取目录路径,并检查它是否为空。
如果它不为空,则 foreach 循环将循环遍历目录中每个文件的完整路径数组。它还会将进度条设置为 0 并准备好进行增量
当它循环时,它将运行cli-taskusing Process
问题是:完成循环文件的任务后,我希望它删除该文件并增加进度条。
如果像这样一一进行的话,效果很好:
process.WaitForExit(10000);
File.Delete(fileName);
progressBar1.Increment(1);
Run Code Online (Sandbox Code Playgroud)
但该程序非常慢。因为它会等待每个进程完成后再开始下一个进程。
所以我尝试了下面的代码:
string[] files = Directory.GetFiles(label1.Text);
if (files.Length != 0)
{
progressBar1.Value = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = files.Length;
foreach (string fileName in files)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\Program Files\cli-tool\cli.exe";
startInfo.Arguments = "-i " + "\"" + fileName + "\"" + " -o " + "\"" + label2.Text + …Run Code Online (Sandbox Code Playgroud) 我试图比较一个字符串与一组对象.因此,当字符串值与数组中的该对象匹配时,它将保留它.
这是一个例子:
sample = [#<Model::ID:0x007fcb83ace8a0
type_id: 6,
parent_name: "Old Man",
child_name: "Junior">,
#<Model::ID:0x007fcb83abd0a0
type_id: 6,
parent_name: "Mary",
child_name: "Michelle">,
#<Model::ID:0x007fcb83abce70
type_id: 6,
parent_name: "Ole Bob",
child_name: "Bobby">]
name = "Michelle"
Run Code Online (Sandbox Code Playgroud)
所以我尝试了以下方法:
sample.keep_if { |keep_ele| [keep_ele]["child_name"] == name }
Run Code Online (Sandbox Code Playgroud)
这会返回以下错误:TypeError:没有将String隐式转换为Integer
但是当我在IRB并且输出时sample[1]["child_name"]它等于"Michelle"
那么我试过:
sample.keep_if { |keep_ele| keep_ele["child_name"] == name }
Run Code Online (Sandbox Code Playgroud)
它刚刚返回一个空数组: []
关于如何运行keep_if的任何想法?
我尝试在使用 psycopg2 时使用多行 f 字符串,如下所示:
query = (
f"select tb.id"
f"from someDB.tableA ta"
f"inner join someDB.tableB tb on ta.url = tb.fk_url"
f"where ta.name = '{some_name}'"
f"and tb.type in ('{some_type}')"
f"order by tb.id;"
)
cursor = connection.cursor()
cursor.execute(query)
cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误:
psycopg2.errors.SyntaxError: syntax error at or near "."
LINE 1: select tb.idfrom someDB.tableA tainner join someDB.tableB...
Run Code Online (Sandbox Code Playgroud)
关于如何在 psycopg2 中使用多行 f 字符串有什么想法吗?如果标准多线也可以工作,我就不必使用 f 字符串。如果可能的话,我只是更喜欢它。
我有一个响应字符串,看起来像这样:
"[{\"id\":\"blahbla23sdlkjrwer2345\",\"name\":\"bar\"},{\"id\":\"aselrjdsfsomething\",\"name\":\"foo\"}]"
Run Code Online (Sandbox Code Playgroud)
然后我用了JSON.parse(response_above):
json_parse = JSON.parse(response_above)
=>[{"id"=>"blahbla23sdlkjrwer2345", "name"=>"bar"},
{"id"=>"aselrjdsfsomething", "name"=>"foo"}]
Run Code Online (Sandbox Code Playgroud)
从这里我只想要名称并将它们放入一个数组中.我想出了如何获取名称但不知道如何将其构建为新数组.
要获得"foo"或"bar",我可以这样做:
json_parse[0].fetch("name")
=> "bar"
json_parse[1].fetch("name")
=> "foo"
Run Code Online (Sandbox Code Playgroud)
我不知道如何遍历数组来从JSON响应构建一个新数组,如:
new_array = ["foo", "bar"]
Run Code Online (Sandbox Code Playgroud)
JSON响应可以是动态的,有时我可能只有2个元素,有时我可以有10个元素.我不能硬编码一个值.我需要找到一种方法来遍历该数组,获取每个值的"name"键.
我想删除整个一行id,并name为001和002从样本:
sample = [{"id"=>"000", "name"=>"Bob"},
{"id"=>"001", "name"=>"Sally"},
{"id"=>"002", "name"=>"Spike"},
{"id"=>"003", "name"=>"Junior"},
{"id"=>"004", "name"=>"Tater"}]
remove_ele = ["001","002"]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下方法,但似乎不起作用:
sample.delete_if { |key, value| sample[x]["id"] == remove_ele[x] }
本质上,我正在尝试比较2和其中的任何匹配项,remove_ele只会删除中的整个行/元素sample。
请以最佳方式协助您完成红宝石的制作。
我目前有一个类似于下面的 bash 脚本:
for i in foo_dir/foo_filename.xml,passed,"some string" foo_dir/bar_filename.xml,failed,"another string" foo_dir/foobar.xml,passed,"string"
do
<some bash script>
done
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我在 for 循环中迭代的内容全部在 1 行上。这很烦人。我希望能够将所有正在迭代的元素放置在单独的行上,如下所示(更易于阅读和更改):
我已经在下面尝试过了,但似乎不起作用。
for i in foo_dir/foo_filename.xml,passed,"some string"
foo_dir/bar_filename.xml,failed,"another string"
foo_dir/foobar.xml,passed,"string"
do
<some bash script>
done
Run Code Online (Sandbox Code Playgroud)
有没有办法使用一些额外的语法或我缺少的东西来完成上述操作?
有没有一种简单的方法来迭代2个数组并找到两个数组中完全相同的任何元素值并将其填充到新数组中?
例如:
arr_a = ["a","b","c","d"]
arr_b = ["123","456","b","d","c"]
Run Code Online (Sandbox Code Playgroud)
我想要创建的数组是:
new_arr = ["b","c","d"]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
another_arr = [*arr_a, *arr_b] #combines the 2 arrays
another_arr.select { |e| another_arr.count(e) >1 }.uniq #then find all dupes
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将结果推送到数组.
这是正确的方式吗?有没有想法如何将结果推送到数组?
我想将一个值someValue, 传递到这个 Map 中。并想知道是否有办法做到这一点。\n这个想法是调用getData.get("code")(someValue)它会调用一个函数并传入正确的值。
private static final Map<String, String> getData = Map.ofEntries(\n Map.entry("code", someSearch.getCode(someValue)),\xe2\x80\xa8\n Map.entry("foo", someSearch.bar(anotherValue)\n);\n\n\ngetData.get("code");\n\nRun Code Online (Sandbox Code Playgroud)\n