我想讀取這個xml文件,它被稱爲「clubregisterquick.xml」,然後嘗試將其輸出到列表,以便我可以在測試中運行多個數據。我不知道我的錯在哪裏我的領域顯示你空的調試。相當新的C#嘗試讀取從XML文件到列表用於DDT
<?xml version="1.0" encoding="utf-8"?>
<ClubRegisterQuick>
<Clubs>
<Club>
<Email>[email protected]</Email>
<Clubname>Gambardella GAA</Clubname>
<Clubphonenumber>07348555550</Clubphonenumber>
<Firstname>David</Firstname>
<Lastname>Peoples</Lastname>
<Town>Maghera</Town>
<Country>N.Ireland</Country>
<Location>Down</Location>
<Currency>GBP</Currency>
<Password>password1</Password>
<Clubtype>GAA</Clubtype>
</Club>
<Club>
<Email>[email protected]</Email>
<Clubname>Computer GAA</Clubname>
<Clubphonenumber>06855583733</Clubphonenumber>
<Firstname>Paul</Firstname>
<Lastname>Smyth</Lastname>
<Town>Draperstown</Town>
<Country>Ireland</Country>
<Location>Wicklow</Location>
<Currency>EUR</Currency>
<Password>password1</Password>
<Clubtype>Rugby</Clubtype>
</Club>
</Clubs>
</ClubRegisterQuick>
這是我的代碼,我試圖實現它。我還沒有改變輸入數據的文本,因爲我沒有返回空值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Collections;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace ClassLibrary2
{
[XmlRoot(ElementName = "ClubRegisterQuick")]
public class Root
{
[XmlElement("Clubs")]
public Clubs clubs { get; set; }
}
public class Clubs
{
public List<Club> clubs { get; set; }
}
public class Club
{
[XmlElement("Email")]
public string Email { get; set; }
[XmlElement("Clubname")]
public string Clubname { get; set; }
[XmlElement("Clubphonenumber")]
public string Clubphonenumber { get; set; }
[XmlElement("Firstname")]
public string Firstname { get; set; }
[XmlElement("Lastname")]
public string Lastname { get; set; }
[XmlElement("Town")]
public string Town { get; set; }
[XmlElement("Country")]
public string Country { get; set; }
[XmlElement("Location")]
public string Location { get; set; }
[XmlElement("PhonCurrencye")]
public string Currency { get; set; }
[XmlElement("Password")]
public string Password { get; set; }
[XmlElement("Clubtype")]
public string Clubtype { get; set; }
}
public static class serialize
{
public static T Deserialize<T>(string path)
{
T result;
using (var stream = File.Open(path, FileMode.Open))
{
result = Deserialize<T>(stream);
}
return result;
}
public static void Serialize<T>(T root, string path)
{
using (var stream = File.Open(path, FileMode.Create))
{
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(stream, root);
}
}
public static T Deserialize<T>(Stream stream)
{
var xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(stream);
}
[TestClass]
public class RegisterClubQuick
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[TestInitialize]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "******";
verificationErrors = new StringBuilder();
}
[TestCleanup]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[TestMethod]
public void TheRegisterClubQuickTest()
{
var a = serialize.Deserialize<Root>("clubregisterquick.xml");
driver.Navigate().GoToUrl(baseURL + "/Clubs");
Thread.Sleep(3000);
driver.FindElement(By.XPath("//a[contains(text(),'Register your Club')]")).Click();
Thread.Sleep(3000);
driver.FindElement(By.Name("email")).Clear();
driver.FindElement(By.Name("email")).SendKeys("[email protected]");
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//button[@type='submit'])[2]")).Click();
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//input[@name='clubName'])[2]")).Clear();
driver.FindElement(By.XPath("(//input[@name='clubName'])[2]")).SendKeys("NoteBook1 FC");
Thread.Sleep(3000);
new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[2]/select"))).SelectByText("Hurling");
Thread.Sleep(3000);
driver.FindElement(By.Name("firstName")).Clear();
driver.FindElement(By.Name("firstName")).SendKeys("Paul");
Thread.Sleep(3000);
driver.FindElement(By.Name("lastName")).Clear();
driver.FindElement(By.Name("lastName")).SendKeys("McDonnell");
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//input[@name='clubPhoneNumber'])[2]")).Clear();
driver.FindElement(By.XPath("(//input[@name='clubPhoneNumber'])[2]")).SendKeys("07345656559");
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//input[@name='town'])[2]")).Clear();
driver.FindElement(By.XPath("(//input[@name='town'])[2]")).SendKeys("CarrickFergus");
Thread.Sleep(3000);
new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[8]/select"))).SelectByText("N.Ireland");
Thread.Sleep(3000);
new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[9]/select"))).SelectByText("Armagh");
Thread.Sleep(3000);
new SelectElement(driver.FindElement(By.XPath("//div[@id='pagewrap']/div[3]/div[3]/div/div/div/form/div[10]/select"))).SelectByText("GBP");
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//input[@name='password'])[2]")).Clear();
driver.FindElement(By.XPath("(//input[@name='password'])[2]")).SendKeys("password1");
Thread.Sleep(3000);
driver.FindElement(By.Name("confirmPassword")).Clear();
driver.FindElement(By.Name("confirmPassword")).SendKeys("password1");
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//input[@type='checkbox'])[2]")).Click();
Thread.Sleep(3000);
driver.FindElement(By.XPath("(//button[@type='submit'])[4]")).Click();
Thread.Sleep(5000);
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
}
}
}
感謝您的幫助是不需要 – Dougiee