【问题标题】:write created data names to a file将创建的数据名称写入文件
【发布时间】:2018-01-12 05:41:10
【问题描述】:

您好,我正在运行 selenium webdriver c#,我正在尝试将某些创建的项目写入文件或控制台。

示例: 可以说我正在创造一辆汽车。我为汽车生成了一个车牌,说“08test1234”现在我想将该值写入某个位置,以便以后可以手动检查它。因为有时候我喜欢进去手动找车做一些修改。

我尝试了Console.WriteLine("08test1234");Debug.Print("08test1234");,但无法让它们中的任何一个显示在任何位置。

非常感谢任何帮助。

这是其中一项测试

   [Fact]
        [Trait("2.", "Participants Tests")]
        public void _2_Cancel_Create_Participant()
        {
            Utilities.CorrectLogin(_driver, Utilities.ManageWorkshopsURL);

            _driver.Navigate().GoToUrl(Utilities.CreateParticipantURL);
            Utilities.WaitforElement(_driver, "uiCreateParticipantFormCancelButton", 10);
            _driver.FindElement(By.Id("uiIsAdminUserChk")).SendKeys(Keys.Space);
            _driver.FindElement(By.Id("uiCreateParticipantFormCancelButton")).Click();


            //Deal with Modal
            Utilities.WaitforElement(_driver, "modal-body", 10);
            Assert.Contains("Are you sure you wish to cancel? Any data you have entered will be lost.", _driver.PageSource);
            _driver.FindElement(By.Id("uiConfirmationModalConfirmButton")).Click();

            Assert.Equal(Utilities.ManageWorkshopsURL, _driver.Url);  
  Debug.Write("need to write this text to output");
        }

    public void Dispose()
        {
            _driver.Quit();
        }
    }
}

【问题讨论】:

  • 您是否发现Console.WriteLine("08test1234"); 有任何错误?必须在 Console.WriteLine("08test1234"); 之前发生错误,并且您的程序永远不会到达该行。你能分享你的代码试验吗?
  • 您好,它通过了测试,只是没有向输出写入任何内容。我把它放在测试中
  • 清理并重建您的项目并尝试Console.WriteLine("08test1234"); 并更新我的状态。
  • 嗨 试过了,还是没有任何显示。尝试将其写入 txt 文件会更容易吗?
  • 是的,这可能更容易。

标签: c# selenium webdriver


【解决方案1】:

这是我用来做类似事情的一个。它只是创建一个包含数据的文本文件。

将本地文件路径添加到您的应用配置。

在此示例中,我传入了电子邮件和帐户类型。然后使用下面的方法将这些值传递给一个文件,以便稍后再参考。

然后只需在您的代码中调用它并传递值。

        Call the method: MyPage.SignupEmail(email, acctType);

        Add to App.config: <add key="LocalFilePath" value="C:\\MyPath" />


        using System;
        using System.Configuration;
        using System.IO;
        using TestFramework;


        namespace Tests
        {
            internal class SignupAccounts
            {
                public static void SignupEmail(string email, string acctType)
                {
                    if (!Directory.Exists(ConfigurationManager.AppSettings["LocalFilePath"]))
                    {

                        Directory.CreateDirectory(ConfigurationManager.AppSettings["LocalFilePath"]);
                    }
                    using (
                        StreamWriter writer =
                            File.AppendText(String.Format(ConfigurationManager.AppSettings["LocalFilePath"] + "\\file_{0}.txt",
                                Driver.GetCurrentTimestamp())))
                                //append a timestamp
                    {

                        writer.WriteLine(String.Format("{0}, {1}", "Applicant Email", @email));
                        writer.WriteLine(String.Format("{0}, {1}", "Applicant Acct Type", @acctType));
                        writer.WriteLine("Password: MyPass123");

                    }
                }

            }
        }

【讨论】:

  • 我会试一试,让你知道它是怎么回事。感谢您的帮助
【解决方案2】:

如果您的项目不是控制台应用程序,它不会写入控制台。如果是单元测试,请在显示结果的区域中查找输出链接。我正在使用 Visual Studio,输出显示在 UI 的左下角。如果您单击该链接,它将打开运行结果,其中将显示您的文本输出。话虽如此,我认为最好将这样的内容写入文本文件,以便在需要时保存输出。网上有很多关于如何写入文本文件的教程,这里不再赘述。

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多