优化一个关键词

发布于 2025-01-09 22:45:53 字数 844 浏览 4 评论 0原文

我使用 SilkTest 和 Java 自动化测试。 在此关键字中,我获取列表并将其与预期的进行比较。 有没有办法优化我的代码,因为我已经多次声明了每个列表。

public void vérification_des_listes(String listAttendu) {
    final String[] list1 = listAttendu.split(";", -1);
    final ArrayList<String> listExpected = new ArrayList<>();
    for (final String I : list1) {
        listExpected.add(I);
    }
    System.out.println(listExpected.toString());
    final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();
    final ArrayList<String> listFound = new ArrayList<>();
    for (final Object E : list2) {
        listFound.add(E.toString());
    }
    System.out.println(listFound.toString());
    assertTrue("", listExpected.equals(listFound));

} 

I automate tests using SilkTest and Java.
In this keyword, I get the list and I compare it to the one expected.
Is there a way to optimize my code since I have declared each list multiple times.

public void vérification_des_listes(String listAttendu) {
    final String[] list1 = listAttendu.split(";", -1);
    final ArrayList<String> listExpected = new ArrayList<>();
    for (final String I : list1) {
        listExpected.add(I);
    }
    System.out.println(listExpected.toString());
    final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();
    final ArrayList<String> listFound = new ArrayList<>();
    for (final Object E : list2) {
        listFound.add(E.toString());
    }
    System.out.println(listFound.toString());
    assertTrue("", listExpected.equals(listFound));

} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2025-01-16 22:45:53
  public static void vérification_des_listes(String listAttendu) {
    final String[] list1 = listAttendu.split(";", -1);
    final List<String> listExpected = Arrays.asList(list1);


    final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();

    final List<String> listFound = Arrays.stream(list2).map(Objects::toString).toList();

     Assertions.assertEquals(listExpected, listFound);
  }

您可以利用类数组,还可以进行一些 java 流处理以将对象转换为字符串。另请查看assertj,以更灵活的方式比较集合(可以查看示例 在这里)。

  public static void vérification_des_listes(String listAttendu) {
    final String[] list1 = listAttendu.split(";", -1);
    final List<String> listExpected = Arrays.asList(list1);


    final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();

    final List<String> listFound = Arrays.stream(list2).map(Objects::toString).toList();

     Assertions.assertEquals(listExpected, listFound);
  }

You can take advantage of class Arrays, and also do some java stream processing for converting your objects to String. Also take a look at assertj to compare Collections in a more flexible way (can see an example here).

森林很绿却致人迷途 2025-01-16 22:45:53

而不是

 final String[] list1 = listAttendu.split(";", -1);
 final ArrayList<String> listExpected = new ArrayList<>();
 for (final String I : list1) {
     listExpected.add(I);
 }

您可以使用

final String[] list1 = listAttendu.split(";", -1);
final List<String> listExpected = Arrays.asList(list1);

参见 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#asList(T... )

Instead of

 final String[] list1 = listAttendu.split(";", -1);
 final ArrayList<String> listExpected = new ArrayList<>();
 for (final String I : list1) {
     listExpected.add(I);
 }

you could use

final String[] list1 = listAttendu.split(";", -1);
final List<String> listExpected = Arrays.asList(list1);

See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#asList(T...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文