对于一个实验,我做了这个小程序.它只生成1000万个随机字符串并将它们添加到arraylist中.请注意,ArrayList中并没有有一个初步的能力.
// editors note: added the necessary boilerplate to run,
// and take initial capacity as an optional cmdline arg for easier testing
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class ArrayListTest {
public static void main(String[] args)
{
int initsize = -1;
if (args.length > 0) {
initsize = Integer.parseInt(args[0]);
}
long startTime = System.currentTimeMillis();
List<String> randNums = initsize>=0 ? new ArrayList<>(initsize) : new ArrayList<>();
// final List<String> randNums = initsize>=0 ? new ArrayList<String>(initsize) : new ArrayList<String>();
Random …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个 Tampermonkey 脚本,它会自动将文本输入到某些表单输入字段中。
通常,您只需执行以下操作即可:
myElement.value = "my new text"
Run Code Online (Sandbox Code Playgroud)
问题是,这个表单正在使用 React,我无法直接更改该值,因为它没有设置 React 状态。我如何将所需的数据输入到我的 Tampermonkey 脚本中的这些 React 组件中?
我在 Cypress 中运行测试时遇到此错误:
Timed out retrying after 10050ms: cy.click() failed because this element is not visible:
Run Code Online (Sandbox Code Playgroud)
我怎样才能点击它呢?它只是一个菜单项,我不需要它在我的测试中可见。
我有一个执行TestNG自动化脚本的GUI程序.这意味着用户可以轻松配置某些设置并启动他们想要的自动化脚本.
我需要补充的一件事是能够立即停止正在运行的TestNG流程.像Eclipse中的那样,"终止"按钮会立即停止正在运行的任何内容.
这就是启动TestNG测试的代码如下所示:
public class ScriptRunner implements Runnable {
public void runScript() {
Thread testRun = new Thread(this);
testRun.start();
}
@Override
public void run() {
//various other things are configured for this,
//but they're not relevant so I left them out
TestNG tng = new TestNG();
//While this runs, various browser windows are open,
//and it could take several minutes for it all to finish
tng.run();
}
}
Run Code Online (Sandbox Code Playgroud)
根据评论,tng.run()可能需要几分钟才能完成,它正在执行几项操作,打开/关闭浏览器窗口等.
我怎样才能立即终止进程,就像从IDE运行应用程序一样?
编辑:
根据评论,我正在尝试使用ServiceExecutor,shutDownNow()代码如下所示:
ExecutorService executorService = Executors.newFixedThreadPool(10); …Run Code Online (Sandbox Code Playgroud) 我正在创建一个小应用程序,用于衡量HTML文档加载所需的时间,每隔x秒检查一次.
我在循环中使用jsoup:
Connection.Response response = null;
for (int i = 0; i < totalGets; i++) {
long startTime = System.currentTimeMillis();
try {
response = Jsoup.connect(url)
.userAgent(USER_AGENT) //just using a Firefox user-agent
.timeout(30_000)
.execute();
} catch (IOException e) {
if (e.getMessage().contains("connect timed out")) {
System.out.println("Request timed out after 30 seconds!");
}
}
long currentTime = System.currentTimeMillis();
System.out.println("Response time: " + (currentTime - startTime) + "ms" + "\tResponse code: " + response.statusCode());
sleep(2000);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,无论什么网站,jsoup连接的第一次执行总是慢于后续的一次.
这是我的输出 https://www.google.com
Response time: 934ms Response code: …Run Code Online (Sandbox Code Playgroud) 我试图找出让多个线程在同一个字符串列表中工作的最佳方法.例如,假设我有一个单词列表,我想要多个线程来打印出这个列表中的每个单词.
这就是我想出的.该线程使用while循环,当迭代器具有next时,它会打印出来并从列表中删除它.
import java.util.*;
public class ThreadsExample {
static Iterator it;
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add("comet");
list.add("planet");
list.add("moon");
list.add("star");
list.add("asteroid");
list.add("rocket");
list.add("spaceship");
list.add("solar");
list.add("quasar");
list.add("blackhole");
it = list.iterator();
//launch three threads
RunIt rit = new RunIt();
rit.runit();
rit.runit();
rit.runit();
}
}
class RunIt implements Runnable {
public void run()
{
while (ThreadsExample.it.hasNext()) {
//Print out and remove string from the list
System.out.println(ThreadsExample.it.next());
ThreadsExample.it.remove();
}
}
public void runit() {
Thread thread …Run Code Online (Sandbox Code Playgroud) 我需要一个XPath,它可以找到一个<a>标签或一个<option>标签,每个标签都包含“某物”。
因此,XPath可以匹配
<a attributes='value'>something</a>
Run Code Online (Sandbox Code Playgroud)
要么
<option attributes="value">something</option>
Run Code Online (Sandbox Code Playgroud)
我尝试了这个:
$x("//*[local-name()='a' contains(.,'something') or local-name()='option' contains(.,'something')]")
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
$x("//*[local-name(contains(.,'something'))='a' or local-name(contains(.,'something'))='option']")
Run Code Online (Sandbox Code Playgroud)
但是它们都不起作用。在第一个中,我可以排除contains()并找到标签,但是我需要能够搜索仅包含指定的“内容”文本的那些标签。
我有以下代码:
String nameAndPath = "C:\\example\\folder\\filename.png";
BufferedImage image = addInfoToScreenshot(); //this method works fine and returns a BufferedImage
ImageIO.write(image, "png", new File(nameAndPath));
Run Code Online (Sandbox Code Playgroud)
现在,该路径C:\example\folder\不存在,因此我收到抛出异常并显示消息:(The system cannot find the path specified)
如何让 ImageIO 自动创建路径,或者我可以使用什么方法自动创建路径?
在此代码的先前版本中,我使用 FileUtils.copyFile 来保存图像(以 File 对象的形式),这将自动创建路径。我怎样才能用这个复制它?我可以再次使用 FileUtils.copyFile,但我不知道如何将 BufferedImage 对象“转换”为 File 对象。
有没有办法 - 也许有一个听众 - 截取每个加载的新页面的截图?截取屏幕本身没有问题,我可以这样做,我只需要一种方法让它自动发生,所以我不必在每次点击某些内容之前手动输入screenshto方法.
我正在看WebDriverEventListner监听器,但它似乎无法真正用于检测任何页面加载而没有事先指定将被加载的元素/将加载的页面?
java ×6
arraylist ×1
automation ×1
capacity ×1
cypress ×1
document ×1
file ×1
filepath ×1
html ×1
javascript ×1
jsoup ×1
listener ×1
performance ×1
reactjs ×1
rider ×1
screenshot ×1
selenium ×1
string ×1
tags ×1
tampermonkey ×1
terminate ×1
testng ×1
web-scraping ×1
webdriver ×1
xpath ×1