День добрый!
У меня есть форма с richtextbox, есть класс который я использую для авторизации с помощью библиотеки селениум:
class Authenticator
{
private IWebDriver _driver;
private SendMsgLogBox _sendMsgLogBox;
public Authenticator(IWebDriver driver)
{
_driver = driver;
}
public Authenticator()
{
_sendMsgLogBox = new SendMsgLogBox(null);
}
public void Authenticate(string login, string password)
{
_driver.Navigate().GoToUrl("https://www.111.com/accounts/login/");
_driver.FindElement(By.CssSelector("input[name='username']")).SendKeys(login);
_driver.FindElement(By.CssSelector("input[name='password']")).SendKeys(password);
_driver.FindElement(By.CssSelector("input[name='password']")).SendKeys(OpenQA.Selenium.Keys.Enter);
_sendMsgLogBox.AddMsg("TEST"); // тут возникает ошибка
try
{
String err = _driver.FindElement(By.CssSelector("#slfErrorAlert")).Text;
Console.WriteLine(err);
}
catch (Exception) {
Console.WriteLine("log and pass is good"); }
}
}
И есть отдельный класс, который будет выполнять действия с richtextbox в главной форме:
class SendMsgLogBox
{
Form1 _form;
public SendMsgLogBox(Form1 form)
{
this._form = form;
}
public void AddMsg(string text)
{
DateTime dt = DateTime.Now;
_form.richTextBox1.Text+=dt+" "+text+ Environment.NewLine;
_form.richTextBox1.SelectionStart = _form.richTextBox1.Text.Length;
_form.richTextBox1.ScrollToCaret();
}
}
Когда я пытаюсь с класса Authenticator с помощью класса SendMsgLogBox добавить строчку
_sendMsgLogBox.AddMsg("TEST");
в своей форме - возникает ошибка "Ссылка на объект не указывает на экземпляр объекта.". Я так полагаю, это как-то связанно с моим слабым понимаем классов. Подскажите решение, пожалуйста.
Сама форма:
public partial class Form1 : Form
{
private Authenticator _authenticator;
IWebDriver web;
public Form1()
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--headless"); //проверить IWebDriver web; на уровень выше сунул
web = new ChromeDriver(@"D:/");
web.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
_authenticator = new Authenticator(web);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String login=textBox1.Text;
String password=textBox2.Text;
_authenticator.Authenticate(login, password);
}
}