SendKeys is not putting the value ToolsQA into the textfield - c#

public static void Main(string[] args)
{
IWebDriver driver = new SafariDriver();
driver.Navigate().GoToUrl("https://demoqa.com/automation-practice-form/");
driver.FindElement(By.Id("firstname")).SendKeys("ToolsQA");
}

I figured out the problem. I should use Name, instead of Id.
driver.FindElement(By.Name("firstname")).SendKeys("ToolsQA");

Related

Can I have two entry points in C#

Would it be possible to use two entry points in C# instead of just having the one. For example when I have this:
using System;
namespace learning
{
class cool
{
static void Main(string[] args)
{
}
}
}
Would it be possible to have another entry point such as secondary that the program executes once the main entry point has finished.
You may want to do something like this:
class Program {
public static void EntryPoint1(string[] args) {
// Code
}
public static void EntryPoint2(string[] args) {
// Code
}
public static void Main(string[] args) {
EntryPoint1(args);
EntryPoint2(args);
}
}
Just make sure to not modify args during EnteryPoint1 or if you want to, clone them like this:
class Program {
public static void EntryPoint1(string[] args) {
// Code
}
public static void EntryPoint2(string[] args) {
// Code
}
public static void Main(string[] args) {
EntryPoint1(args.Clone());
EntryPoint2(args.Clone());
}
}
In C#, you specify the entry point using the /main: compiler option.
Imagine that the code containing containing two main() methods as follow :
namespace Application {
class ClassA {
static void main () {
// Code here
}
}
class ClassB {
static void main () {
// Code here
}
}
To use ClassA.main() as your entry point, you would specify the following when compiling:
csc /main:Application.ClassA hello.cs
You can only have a single entry point, but you can write two separate methods, call the first one, and then the second one. You will achieve what you're describing.
using System;
namespace learning
{
class cool
{
static void Main(string[] args)
{
PartOne();
PartTwo();
}
void PartOne() {
// Something happens here
}
void PartTwo() {
// Something else happens here
}
}
}
Additionally (depending on how the program starts up) you can send in arguments to specify which method you want to execute (if you don't need both of them to execute). Then you can just do an "if/else" statement that will decide which method to run depending on the arguments passed into Main

How do you call non static methods from static Main if Main is the starting point

I am new to C# so I hope I am asking this correctly.
I created a new C# project and in it is:
static void Main(string[] args)
{
Consol.Write(myFirstInt());
}
I created a method:
public int myFirstInt()
{
return 5;
}
That is called from the Main. I get that I can't call myFirstInt() because it is not static. However, if Main is the starting point for the program and always has to be static, how do you call non static methods?
You'd have to create a new instance of the class you're running your code in.
Say your code looks like this:
public class YourProgram {
public int myFirstInt(){
return 5;
}
public static void Main(string[] args){
// ...
}
}
You'd have to create a new instance of the YourProgram class like so:
public class YourProgram {
public int myFirstInt(){
return 5;
}
public static void Main(string[] args){
var yourProgram = new YourProgram();
Console.Write(yourProgram.myFirstInt());
}
}
Side note: You made a typo in your code. You wrote Consol.Write which sould be Console.Write. I corrected it in the code above.
Supposing you declared the myFirstInt method in the Program class you just have to do
var program = new Program();
Console.Write(program.myFirstInt());
although i think you just need to change myFirstInt to static

how to call element in static void?

I'm doing a little selenium program with c#. I want to wait max 5 seconds to interact button or something if its visible. I've made a code for it but I cannot call that code inside static void main it says an object is required non-static field. how do I fix this ?
an error : An object reference is required for the non-static field, method, or property 'Program.waitForPageUntilElementIsVisible(By, int)
class Program
{
public IWebDriver driver;
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.mail.com/int/");
IWebElement login = driver.FindElement(By.Id("login-button"));
login.Click();
IWebElement email = driver.FindElement(By.Id("login-email"));
waitForPageUntilElementIsVisible(By.Id("login-email"), 5);
email.SendKeys("CarlosdanielGrossen95#mail.com");
}
public IWebElement waitForPageUntilElementIsVisible(By locator,int maxseconds)
{
return new WebDriverWait(driver, TimeSpan.FromSeconds(maxseconds))
.Until(ExpectedConditions.ElementExists((locator)));
}
}
}
An object reference is required for the non-static field, method, or property 'Program.waitForPageUntilElementIsVisible(By, int)
Make the method static.
public static IWebElement waitForPageUntilElementIsVisible(By locator,int maxseconds)
{
return new WebDriverWait(driver, TimeSpan.FromSeconds(maxseconds))
.Until(ExpectedConditions.ElementExists((locator)));
}
Define this method in a separate class and call this method with PageWait Object.
public class PageWait
{
public IWebElement waitForPageUntilElementIsVisible(By locator,int maxseconds)
{
return new WebDriverWait(driver, TimeSpan.FromSeconds(maxseconds))
.Until(ExpectedConditions.ElementExists((locator)));
}
}
class Program {
public IWebDriver driver;
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.mail.com/int/");
IWebElement login = driver.FindElement(By.Id("login-button"));
login.Click();
IWebElement email = driver.FindElement(By.Id("login-email"));
new PageWait().waitForPageUntilElementIsVisible(By.Id("login-email"), 5);
email.SendKeys("CarlosdanielGrossen95#mail.com");
}
}

Call Methode in process from c#-project

I want to call a public Methode(Send) in a process from my c# project!
This is the process with the Methode that i want to call :
namespace Test123
{
class Program
{
static void Main(string[] args)
{
while(true)
{
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
}
I know how to get the process but not how to invoke the methode!
I already searched on other websites and i dinĀ“t found anything that helps me.
Create a instance of the program in the static and call the method.
class Program
{
static void Main(string[] args)
{
var p = new Program();
while(true)
{
p.Send();
}
}
public void Send()
{
Console.WriteLine("Test");
}
}
If your in the same Program class, you can just call the method. The other approach listed where you declare a new Program, would be needed if you were in a different class.
static void Main(string[] args)
{
while(true)
{
Send();
}
}

How to access a variable in Program class (console app)

I would like to know how to access a public var in Program class of the console app.
class Program
{
public static string Name { get; set; }
static void Main(string[] args)
{
// Some code here
}
}
static class Settings
{
static public void DoJob()
{
// Access Name of Program ?
}
}
Sure you can do this, But the args is a string array and the property Name is a string variable, So you need to assign one value from args to the Name. Or use String.Join to get all values to Name with a delimiter.
Since the Name is static variable no instance is needed to access the variable. You will get the value through Program.Name in the static class. Now see the code:
In Main getting value from args to Name
public static string Name { get; set; }
static void Main(string[] args)
{
Name = args[0]; // taking the First value from the args array
//or use String.Join to get all elements from args
string delemitter = "";
Name = String.Join(delemitter, args);
}
In Static class assign value of Name to a local variable:
static class Settings
{
static public void DoJob()
{
string localVar = Program.Name;
}
}

Categories

Resources