2011-03-18 66 views

回答

6

我刚创建了一个单独的测试,并将其放在所有测试的顶部。将基础url存储到一个变量,现在我可以通过$ {variable_name}在其他测试用例中访问它。

+1

这不是工作:

Selenium.prototype.doBaseURL = function(strTarget, strValue) { /** * Enables a more predictive Base URL when running a test suite with multiple test cases. * On first usage it wil fill a stored variable 'baseURL' either with the given strTarget, * or when not given with the Base URL as shown on the Selenium IDE UI. The Selenium IDE UI field * Base URL is updated to reflect the (new) base URL. * In each subsequent test case with a baseURL command the stored variable 'baseURL' will determine * the base. * * <p> Best to use as first command in each test case and only in the first test case a strTarget * argument, in that way the test cases of a test suite will no longer depend on their 'hidden' * '<link rel="selenium.base" href="http://www.something.com/" /> * construction, that makes test cases effectively having an absolute base URL. * </p> * * @param strTarget a Base URL value to be used in the test case (e.g. https://www.google.com/) */ // pushes the given strTarget into the selenium.browserbot.baseUrl // and makes the new Base URL value visible on the Selenium IDE UI. // A subsequent open command will use this new Base URL value LOG.debug("begin: doBaseURL | " + strTarget + " | " + strValue + " |"); // note: window.location.href = strTarget; causes the browser to replace Selenium-IDE UI with the given url // used that knowledge to manipulate the Selenium IDE UI as if a manual Base URL change was done. LOG.debug("window.location.href= [" + window.location.href + "]"); LOG.debug("window.editor.app.getBaseURL() gives [" + window.editor.app.getBaseURL() + "]"); if (strTarget && (strTarget.substring(0,1)!="$")) { // only when strTaget has a value strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + strTarget + "]"; selenium.browserbot.baseUrl = strTarget.toString(); // set the visible Base URL value on the Selenium IDE UI to the new value window.editor.app.setBaseURL(strTarget); LOG.debug("updated into: " + window.editor.app.getBaseURL()); // remember this value for future calls storedVars["baseURL"] = window.editor.app.getBaseURL(); LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]"); } else { // no value => take storeVars["baseURL"] when set; // otherwise take Selenium IDE Base URL field. if ((storedVars["baseURL"] != undefined) && (storedVars["baseURL"] != "")) { strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + storedVars["baseURL"] + "]"; selenium.browserbot.baseUrl = storedVars["baseURL"]; // set the visible Base URL value on the Selenium IDE UI to the new value window.editor.app.setBaseURL(storedVars["baseURL"]); LOG.debug("updated from storedVars[\"baseURL\"]"); } else { strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + window.editor.app.getBaseURL() + "]"; selenium.browserbot.baseUrl = window.editor.app.getBaseURL(); // remember this value for future calls storedVars["baseURL"] = window.editor.app.getBaseURL(); LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]"); } } LOG.info(strValue); LOG.debug("end: doBaseURL | " + strTarget + " | " + strValue + " |"); } 

安装用户扩展脚本的 “使用用户扩展使用Selenium-IDE” 部分SeleniumHQ文档中解释我。我试图把$ {variable_name}放在基本URL和打开命令中,并且在两种情况下它都不会被我以前设置的值替换。你能详细谈谈你是如何做到这一点的吗?谢谢! – 2011-07-18 20:52:56

+0

Feh。我有反驳“存储”的论据。 – 2011-07-18 20:58:10

+0

有人可以提供有关此策略的更多详细信息吗?我无法让它工作。 Selenium似乎没有评估链接元素中的'$ {variables}',并且在以后的测试中将一个值“存储”回到'selenium.base'中似乎不起作用。 – 2012-01-18 23:59:21

12

测试用例HTML文件负责设置基本URL。从Selenium IDE中,您可以覆盖测试用例的基本URL。另一方面,测试套件只是您的测试用例的一个包。

从您的测试套件中引用的每个测试用例文件中移除基本URL设置。然后手动为测试套件设置基本URL并运行套件。

所讨论的行是每个测试用例文件的头:

<link href="http://server-name/" rel="selenium.base"/> 

如果缺少,则基本URL自动重写它。

+1

太棒了,适合我。 – 2013-01-29 03:45:43

32

如果您在单个套件中有很多测试用例,则更改每个套件的基本URL是一种痛苦。相反,请为每个需要切换的基本网址创建一个单独的案例。例如,我将store https://testing.site.com/作为myEnvironment另存为测试用例SetEnvTesting。我为我的生产站点SetEnvProduction创建了另一个案例。

然后,将${myEnvironment}插入测试用例中每个相对URL的开头。例如,open ${myEnvironment}/login.aspx。 (如果您已经进行了大量测试,这可能会很痛苦,我建议您从现在开始添加它。)然后,只需将set-environment测试用例放在测试套件的开头即可。要将整个套件切换到另一个基本URL,只需在开始时放置一个不同的设置环境案例。另一个好处是,您可以通过在中间放置不同的集环境案例来切换套件中间的环境。

编辑:使用SetEnvironment测试用例的示例。

的SetEnvironment情况: enter image description here

一个下面的测试情况的​​一个例子。 enter image description here

通知

  1. 如何CurrentEnvironment变量被使用。您可以为套件中的每个案例做到这一点。另外,您可以让每个独立的测试套件都使用相同的SetEnvironment案例,因此它们都可以一起切换。

  2. 该基地网址变得不相关。本质上,你压倒了它。

我希望这很有帮助!

+0

你可以发布myEnvironment脚本吗? – Kayser 2013-01-24 13:51:24

+0

@Kayser我编辑的帖子添加一个例子。希望能帮助到你。 – arwenvh 2013-01-30 22:12:16

+1

@arwenvh回复:“改变每个基本URL是一种痛苦” 我使用这个linux/bash命令来替换我的测试用例中的所有有问题的行(都带有.tc扩展名)'find/path/to/your/testcases -type f -name“* .tc”-exec sed -i“s /.* selenium.base。*/<! - selenium.base被整洁脚本删除 - > /”'{}'\; ' – 2015-02-04 04:40:32

9

我发现它非常令人沮丧我不能只是有一个空白的基本URL,并让它使用我已经打开的任何页面作为测试的开始。什么是能够记录相对网址,当它迫使你到硬编码一个基地网址每个相对网址的前缀(从而把它们变成absoulte网址)?

扩展在其他的答案,并从这个问题:In Selenium IDE, how to get the value of the base url

可以存储在一个变量的当前域,然后用这个来代替硬编码的一个。这可以让你登录到你想运行测试的服务器上,点击播放,它会运行测试,而不用把所有baseURL废话都重定向到错误的服务器上。

就在第一个脚本开始在你的浴室使用以下命令: store current server

,同时继续使用http://${host}/语法,你在你的脚本需要每真正相对 URL。

+1

我更喜欢使用** window.location.origin **,因为它将包含协议和可选端口,所以我不需要对它们做出假设。 – 2016-11-24 09:05:32

1

我试过接受的答案,它对硒IDE有效,但在运行stand-alone server时无效。

最后,我把值放在user-extensions.js里面,然后用storeEval取回值。然后,可以在不同情况下检索相同的值。

编辑:

在用户extensions.js,添加以下

var selenium_base_url = "http://example.com" 

在HTLM套件,添加以下

<tr> 
    <td>storeEval</td> 
    <td>selenium_base_url</td> 
    <td>case_base_url</td> 
</tr> 
<tr> 
    <td>open</td> 
    <td>${case_base_url}/case_path?a=1&b=2</td> 
    <td></td> 
</tr> 

这可以看第一奇数视线,但实际上user-extension.js可以在执行stand-alone server之前即时修改,因此您可以在真实中使用不同的基本URL QA场景。

+0

Hello @Walty Yeung,你有没有关于你的.js扩展的例子? TKS – 2015-08-18 18:14:14

0

嗯,我觉得这是一个简单的解决方案:

  • 在第一次测试(测试套件)调整 <link rel="selenium.base" href="http://google.com" />所示卸下(使用文本编辑器需要

  • 在下面的测试中或类似): <link rel="selenium.base" ... />

0

如果你没事双击多次,

  1. 双击顶部,将其设置为您想要的网址。
  2. 现在双击顶部,中间,底部。
  3. 现在双击底部,中间,顶部。
0

我创建了一个用户扩展命令'doBaseURL'来操纵Selenium IDE字段'基本URL'。 在测试套件的每个测试案例中,我添加了这个命令| baseURL | | |。在曲面下面,它设置一个名为baseURL的存储变量。 在第一个'baseURL'命令之前,可以通过测试用例命令为存储变量'baseURL'提供一个值。 该方法覆盖每个测试用例文件的<link href="http://server-name/" rel="selenium.base"/>设置。 对我来说,这个工作正常,因为现在Selenium IDE反映了测试用例将使用(或者在发生错误时正在使用)的基本URL。

的Java脚本是:http://www.seleniumhq.org/docs/08_user_extensions.jsp

相关问题