如何让selenium脚本使用值列表

How to get selenium script to use a list for values?

本文关键字:列表 脚本 selenium      更新时间:2023-09-26

我试图创建一个硒脚本登录到一个网站的用户名和密码完成一个任务,注销然后重新开始,但有多个不同的用户名/密码。是否有可能让脚本从文本文件或其他东西中使用值?

<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
	<td>open</td>
	<td>/common/oauth2/authorize?client_id/td>
	<td></td>
</tr>
<tr>
	<td>assertTitle</td>
	<td>Sign in to your account</td>
	<td></td>
</tr>
<tr>
	<td>type</td>
	<td>id=cred_userid_inputtext</td>
	<td>example@example.com</td>
</tr>
<tr>
	<td>click</td>
	<td>id=cred_password_inputtext</td>
	<td></td>
</tr>
<tr>
	<td>type</td>
	<td>id=cred_password_inputtext</td>
	<td>password1</td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>id=cred_sign_in_button</td>
	<td></td>
</tr>
<tr>
	<td>click</td>
	<td>css=span.ms-Icon--outlook</td>
	<td></td>
</tr>
<tr>
	<td>select</td>
	<td>id=selTz</td>
	<td>label=‎(UTC-08:00)‎ Pacific Time ‎(US &amp; Canada)‎</td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>css=span.signinTxt</td>
	<td></td>
</tr>
<tr>
	<td>click</td>
	<td>xpath=(//button[@type='button'])[12]</td>
	<td></td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>xpath=(//button[@type='button'])[51]</td>
	<td></td>
</tr>
<tr>
	<td>click</td>
	<td>//table[@id='use_another_account']/tbody/tr/td/table/tbody/tr[2]/td[2]/div</td>
	<td></td>
</tr>
</tbody></table>
</body>
</html>

当然可以从外部源读取并运行您的自动化测试。如果您有很多凭证,我会建议您直接与测试数据库对话以检索该数据,然后将其用于您的自动化测试。我不知道javascript或python,但我相信如果你谷歌,你可以找到一个解决方案,如何做到这一点。

如果这太复杂,那么您可能更喜欢从像.csv这样的文件中读取。Python或javascript可能会有一些框架,您可以使用它们来读取。csv文件。最坏的情况是,你将不得不自己编写逻辑来读取文件,我相信这个文件已经存在于网络上了。

无论您选择什么方法,只需使用代码读取该实体,将值存储在变量中,然后在测试中应用这些变量。下面是我使用c#进行测试的示例。它运行从外部源读取的许多' accessionnumber '。

        [Test, TestCaseSource("GetDataList")]
    [Property("PBI#", "35656")]
    public void GivenPopupItemIsOpen_WhenTogglingPopup_ThenPopupOpensAndCloses(string accessionNumber)
    {
        var studentAssesmentPage = OpenAdminResetPageAndGoToBookletLocation(accessionNumber);
        studentAssesmentPage.ShortTextPopup.OpenPopUp();
        studentAssesmentPage.ShortTextPopup.ClosePopUp();
        Assert.IsFalse(studentAssesmentPage.ShortTextPopup.IsPopUpOpen(), "The pop up did not close after trying to close the pop up");
    }