I get an error message on trying to run this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
using CourseManagementUIAutomation_02.Hooks;
using CourseManagementUIAutomation_02.Pages;
using NUnit.Framework;
namespace CourseManagementUIAutomation_02.Step_Definitions
{
[Binding]
public class StudentStepDefinitions
{
//01). Add Dependency To Reference The "context.cs" File - "using CourseManagementUIAutomation_02.Hooks;"
//02). "Call" Or "Use" The Conext File Created In The "Hooks" Folder
Context context;
//06c). "Call" Or "Use" The studentPage File Created In The "Pages" Folder
StudentPage studentPage;
//03). Create A Constructor; As Done Before In Another Class
//06d). Add "StudentPage _studentPage" As An Additional Parameter For The Argument
//06d). In The Method Below - Just As "Context _context" Was Used As An Argument
//06d). For The Method Earlier
public StudentStepDefinitions(Context _context, StudentPage _studentPage)
{
context = _context;
//06e). Add "studentPage = _studentPage;" In The Curly Brackets For This Method
//06e). As Was Done For "context = _context" Earlier
//06f). Go To The Comment On Line 81 Of This ".cs" File
studentPage = _studentPage;
}
[Given(@"That The Course Management System Is Loaded")]
//04). Replace "ScenarioContext.Current.Pending();" With
//05). "context.Initialize();
//05a). The code - "context.Initialize(); Launches The Browser & "Binds" It To The
//05a). The "Student.feature" File
//05b). VIP - Any Step In This File That You Doesn't Have The Code - "ScenarioContext.Current.Pending();"
//05b). VIP- Signifies That The Method Has Been Bound Successfully.
//05b). VIP- In Other Words, To Successfully Bind The Method To The Corresponding Step In The Feature File,
//05b). VIP- You Need to Replace The Replace Code - "ScenarioContext.Current.Pending();" With
//05b). VIP- With The Appropriate "context. ......." Code
public void GivenThatTheCourseManagementSystemIsLoaded()
{
context.Initialize();
}
//06). Go To The Corresponding Step In "Student.Feature" File
//06a). Information To Bind This Step Is In The "StudentPage.cs" File.
//06b). So, It Has To Be Referenced. Do This By Implementing The Dependency - "using CourseManagementUIAutomation_02.Pages;"
//06c).
[When(@"A User Clicks On The Student Link")]
public void WhenAUserClicksOnTheStudentLink()
{
//07). Replace "ScenarioContext.Current.Pending();" With
//07). "studentPage.ClickOnStudentsLink();"
studentPage.ClickOnStudentsLink();
}
[When(@"A User Clicks On The Create New Link")]
//08). Go To The "Student Feature" File. On Line 18, Right Click On "Go To Definition"
//08b). V/Studio Should Bring You To This Section Of The Code.
//08c). You Require A Method That Will "Instruct" Selenium To Click On The
//08d). "Create New" Hyper Link In The SUT
//08e). VIP - This Is Why It Is "Super" Important To Give The Methods Quite Descriptive Names
public void WhenAUserClicksOnTheCreateNewLink()
{
//09). Replace ""ScenarioContext.Current.Pending();" With
//09). "studentPage.ClickOnCreateNewStudentsLink();"
studentPage.ClickOnCreateNewStudentsLink();
}
[When(@"A User Fills In The Family Name Field With (.*)")]
//10). Go To The "Student Feature" File. On Line 19, Right Click On "Go To Definition"
//10). V/Studio Should Bring You To This Section Of The Code.
//10). You Require A Method That Will Fill In The Required Details In The
//10). "Family Name" Field In The SUT
public void FamilyNameField(string familyName)
{
//11). Replace ""ScenarioContext.Current.Pending();" With
//11). "studentPage.FillInFamilyNameField(familyName);"
studentPage.FillInFamilyNameField(familyName);
}
[When(@"A User Fills In The First Name Field With (.*)")]
//12). Go To The "Student Feature" File. On Line 20, Right Click On "Go To Definition"
//12). V/Studio Should Bring You To This Section Of The Code.
//12). You Require A Method That Will Fill In The Required Details In The
//12). "First Name" Field In The SUT
public void FirstNameField(string firstName)
{
//13). Replace ""ScenarioContext.Current.Pending();" With
//13). "studentPage.FillInFirstNameField(firstName);"
studentPage.FillInFirstNameField(firstName);
}
[When(@"A User Fills In The Enrollment Date Field With (.*)")]
//14). Go To The "Student Feature" File. On Line 21, Right Click On "Go To Definition"
//14). V/Studio Should Bring You To This Section Of The Code.
//14). You Require A Method That Will Fill In The Required Details In The
//14). "Enrollment Date" Field In The SUT
public void WhenAUserFillsInTheEnrollmentDateFieldWith(string enrollmentDate)
{
//15). Replace ""ScenarioContext.Current.Pending();" With
//15). "studentPage.FillInEnrollmentDateField(enrollmentDate);"
studentPage.FillInEnrollmentDateField(enrollmentDate);
}
[When(@"A User Clicks On The Create Button")]
//16). Go To The "Student Feature" File. On Line 22, Right Click On "Go To Definition"
//16). V/Studio Should Bring You To This Section Of The Code.
//16). You Require A Method That Will "Instruct" Selenium To Click On The "Create Button"
//16). In The SUT
public void WhenAUserClicksOnTheCreateButton()
{
//17). Replace ""ScenarioContext.Current.Pending();" With
//17). "studentPage.ClickOnCreateStudentButton();"
studentPage.ClickOnCreateStudentButton();
}
[Then(@"A New Student Record Amoo Should Be Created")]
//18). Go To The "Student Feature" File. On Line 23, Right Click On "Go To Definition"
//18). V/Studio Should Bring You To This Section Of The Code.
//18). You Require A Method That Will Verify That The Required Student Record Has Been
//18). Created Successfully In The SUT
//18a). This Method Will Be Data Driven In The Future; Using A Table To Do This.
public void ThenANewStudentRecordAmooShouldBeCreated()
{
//19). Replace ""ScenarioContext.Current.Pending();" With
//19). "string expectedResult = "http://localhost/CourseManagementSystem/students";
//19). string actualResult = studentPage.VerifyNewlyCreatedStudentRecord();
//19). Assert.AreEqual(expectedResult, actualResult);"
//19a). Add The Dependency In Order To Use The "Assert" Class -
//19b). Using Microsoft.VisualStudio.TestTools.UnitTesting;
string expectedResult = "http://localhost/CourseManagementSystem/students";
string actualResult = studentPage.VerifyNewlyCreatedStudentRecord();
Assert.AreEqual(expectedResult, actualResult);
//19c). Right Click On The Method - "VerifyNewlyCreatedStudentRecord()"
//19d). Click On "Go To Definitions".
//19d). V/Studio Will Take You To The "StudentPages.cs" File
//19e). To Avoid Selenium Retrieving "A Blank Space" With The URL,
//19e). Add The ".Trim();" Method To The Code - "return context.driver.Url;"
//19f). So, You Will Have - "return context.driver.Url.Trim();" As The New
//19f). Line Of Code.
//20). You Can Execute The Test Scenario Now. Rebuild The Test.
//20). By Going To "Test >> Test Explorer". Click On The "White Triangle Under "Test"
//20). In Order To Drill Down To The Available Scenarios In The Project.
//20). You Can Select The Particular Scenario(s) You Would Like To Run.
}
//New Scenario - "Student_02_Verify That A New Student Record Can Be Created (Table Format)"
[When(@"A User Fills In A New Student Form Page With The Data Below:")]
public void WhenAUserFillsInANewStudentFormPageWithTheDataBelow(Table table)
{
ScenarioContext.Current.Pending();
}
[Given(@"That The CourseManagementSystem Is Fully Loaded")]
public void GivenThatTheCourseManagementSystemIsFullyLoaded()
{
ScenarioContext.Current.Pending();
}
[When(@"A User Fills In A New Student Form Page With (.*), (.*), (.*) fields")]
public void ANewStudentFormPageWithFields(string familyName, string firstName, string enrollmentDate)
{
ScenarioContext.Current.Pending();
}
[When(@"And A User Clicks On The Create Button")]
public void WhenAndAUserClicksOnTheCreateButton()
{
ScenarioContext.Current.Pending();
}
[Then(@"A New Student Record (.*) Should Be Created")]
public void ThenANewStudentRecordShouldBeCreated(string p0)
{
ScenarioContext.Current.Pending();
}
[Then(@"A User Clicks On The Create Button")]
public void ThenAUserClicksOnTheCreateButton()
{
ScenarioContext.Current.Pending();
}
}
}
Error Message:
“OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"css selector","selector":"#LastName"}”
I get this error message on running the test scenario in test explorer.
SCREEN SHOTS:
ACTION ALREADY TAKEN TO FIX THE ISSUE:
1). I have carefully checked the line of code that appears to contain the error message.
Line 116 Of “StudentStepDefinitions” Seems To Contain The Definition For The “FamilyName”
public void FamilyNameField(string familyName)
That code seems to be ok. No errors are generated on that piece of code.
2). I have checked Google for more information about the error.
Information I have found so far:
- 1 Coder suggests applying a wait of 5000ms.
(Link: https://stackoverflow.com/questions/44572164/openqa-selenium-nosuchelementexception)
3). There is a suggestion to use the “expected conditions class” (Link: https://stackoverflow.com/questions/44179554/openqa-selenium-nosuchelementexception-c-sharp-selenium)
4). There is another suggestion to use “Driver.FindElement(By.LinkText(“…..”))
I am not sure how best to proceed, please. That is where I require assistance after doing this bit of research, please.
Any suggestions to fix the issue will be highly apprciated, please.
Many Thanks.
Error When Trying To Add A Wait On The Field That Contains The Data: