Welcome to the Simplilearn Community

Want to join the rest of our members? Sign up right away!

Sign Up

Selenium Certification Training || Sep 4, Oct 3 || Harish Pandalangatt

Tushti Kapoor

Customer
Customer
I declared a global variable and a static variable
public int a=10;
static int b=20;

I created a static method
public static void printb() //static method
{
System.out.println("Printing B");
System.out.println(a); //But this gave me an error, saying that "Change 'a' to static"
System.out.println(b);
}


Attached is the screenshot of error.
Below is the complete code:


package demo;

public class Staticvarexample {
public int a=10; //global variable
static int b=20; //static variable, doesn't need object to be accessed

public void printa()
{
System.out.println("Printing A");
System.out.println(a);
}

public static void printb() //static method
{
System.out.println("Printing B");
System.out.println(b);
System.out.println(a);
}


public static void main(String args[])
{
//creation of object, classname objectname= new classname(), to access the methods available in the class
Staticvarexample obj1=new Staticvarexample();

//calling methods using object
obj1.printa();

//calling static method and printing static var
System.out.println(Staticvarexample.b);
Staticvarexample.printb();

}

}
 

Attachments

  • staticVariable error.png
    staticVariable error.png
    38.1 KB · Views: 7

harishp1906

Well-Known Member
I declared a global variable and a static variable
public int a=10;
static int b=20;

I created a static method
public static void printb() //static method
{
System.out.println("Printing B");
System.out.println(a); //But this gave me an error, saying that "Change 'a' to static"
System.out.println(b);
}


Attached is the screenshot of error.
Below is the complete code:


package demo;

public class Staticvarexample {
public int a=10; //global variable
static int b=20; //static variable, doesn't need object to be accessed

public void printa()
{
System.out.println("Printing A");
System.out.println(a);
}

public static void printb() //static method
{
System.out.println("Printing B");
System.out.println(b);
System.out.println(a);
}


public static void main(String args[])
{
//creation of object, classname objectname= new classname(), to access the methods available in the class
Staticvarexample obj1=new Staticvarexample();

//calling methods using object
obj1.printa();

//calling static method and printing static var
System.out.println(Staticvarexample.b);
Staticvarexample.printb();

}

}

Hi Tushti,

Good observation, the reason you got the error was, A static method can access only static data. It is a method which belongs to the class and not to the object(instance). A static method can access only static data. It cannot access non-static data (instance variables). So you have to make it as a static variable to access it inside static method.

Please let me know if you have any other questions.
 

Tushti Kapoor

Customer
Customer
Hi Tushti,

Good observation, the reason you got the error was, A static method can access only static data. It is a method which belongs to the class and not to the object(instance). A static method can access only static data. It cannot access non-static data (instance variables). So you have to make it as a static variable to access it inside static method.

Please let me know if you have any other questions.
Got it. But the main method "public static void main(String args[])" is also static, right? How is it able to access global variables while the other method I created could not.
 

harishp1906

Well-Known Member
Hi Tushti,

Main method is used to start or instantiate your objects for the class. In main method we are able to access static and non static methods because we are using an object to call the methods. If we are using a static method for the class we can just call the method name alone because it is created for the same class.

Hope I have clarified your question.
 

harishp1906

Well-Known Member
Dynamic Xpath for VIEW ALL button ( Go to https://www.flipkart.com/ -> Scroll a bit -> View All button is displaying)
Added a snapshot for reference.
Query: Please share the dynamic xpath of this "View All" button
Hi Jyotsna,

Good question! There are lot of ways to get the xpath for the element which we might have seen in the class as well using xpath axes methods.

Since view all is used across 8 - 9 positions in the page, we can go by indexing the element using text () method.

xpath =(//a[text()='VIEW ALL'])[1]
Absolute xpath (Just for reference) = /html/body/div[1]/div/div[3]/div[3]/div[1]/div/div[1]/div/div[2]/a

Let me know if this helps.
 

harishp1906

Well-Known Member
what is Smoke and regression testing? how to write test cases on that? please advise
Hi Eswarlal,

Good question!

Smoke testing is an approach which is usually carried out during the initial development stages of the Software Development Life Cycle(SDLC) to make sure that the core functionalities of a program are working fine without any issues. It is executed before any detailed functional tests are done on the software.

Smoke testing is also called as Build Verification Test.

Regression testing is the verification of “bug fixes or any changes in the requirement” and making sure they are not affecting other functionalities of the application. Regression testing is effective on automation and usually performed after some modifications have been made in the software build after requirement changes or bug fixes.

This can be purely achieved with the help of Automation.

Talking about deriving the Test cases, You can have all the core functionalities for an application (Ex: Login, Placing an order in eCommerce application) to work in Smoke Testing suite, where as in Regression you can have the detailed functionalities coverage. (Ex: Adding item to cart from different categories, Updating cart etc.,)
 
Hi Eswarlal,

Good question!

Smoke testing is an approach which is usually carried out during the initial development stages of the Software Development Life Cycle(SDLC) to make sure that the core functionalities of a program are working fine without any issues. It is executed before any detailed functional tests are done on the software.

Smoke testing is also called as Build Verification Test.

Regression testing is the verification of “bug fixes or any changes in the requirement” and making sure they are not affecting other functionalities of the application. Regression testing is effective on automation and usually performed after some modifications have been made in the software build after requirement changes or bug fixes.

This can be purely achieved with the help of Automation.

Talking about deriving the Test cases, You can have all the core functionalities for an application (Ex: Login, Placing an order in eCommerce application) to work in Smoke Testing suite, where as in Regression you can have the detailed functionalities coverage. (Ex: Adding item to cart from different categories, Updating cart etc.

Hi Eswarlal,

Good question!

Smoke testing is an approach which is usually carried out during the initial development stages of the Software Development Life Cycle(SDLC) to make sure that the core functionalities of a program are working fine without any issues. It is executed before any detailed functional tests are done on the software.

Smoke testing is also called as Build Verification Test.

Regression testing is the verification of “bug fixes or any changes in the requirement” and making sure they are not affecting other functionalities of the application. Regression testing is effective on automation and usually performed after some modifications have been made in the software build after requirement changes or bug fixes.

This can be purely achieved with the help of Automation.

Talking about deriving the Test cases, You can have all the core functionalities for an application (Ex: Login, Placing an order in eCommerce application) to work in Smoke Testing suite, where as in Regression you can have the detailed functionalities coverage. (Ex: Adding item to cart from different categories, Updating cart etc.,)
thank you Harish
 
why we need to get cookies and delete it. the script works even we dont get cookies.
please share the difference.
also pls add any realtime scenario that cookies being managed.

driver.findElement(By.xpath("//button[@type='submit']")).click();

//System.out.println(driver.manage().getCookies());

driver.manage().deleteAllCookies();
 
Top