简体   繁体   中英

changing the url in the extended class

I am trying to learn Java. I extend from a class and each extended page goes to its own url. How can I change the "url" as variable? Here is my code:

public class BaseTest {
    
        
        public static String baseUrl;
        public void setUp() throws InterruptedException  {
           //there is some code for setup.
            driver.get("https://www.hurriyet.com.tr/");
            
        }
 other page is extends like this:

public class sample1page extends BaseTest{
    @Test
    public void method() throws InterruptedException {
        
        //here ı need to change url
    }
public class sample2page extends BaseTest{
    @Test
    public void edituseracco() throws InterruptedException {
        
        //here ı need to change url
    }

For a static variable, single copy of variable is created and shared among all objects at the class level.

So, parent class and all child classes share the same variable. Now one change reflects everywhere.

public void method() throws InterruptedException {
  BaseTest.baseUrl = "http://hollo.com";
}

Moreover, if you want to declare the same (name and type) variable, inside every child class, you can avoid the above situation.

class Sample1page extends BaseTest {
    public static String baseUrl;
    @Test
    public void method() throws InterruptedException {

        baseUrl = "http://example.com";
    }
}

But, this time similar child classes shares the same variable if you declare the variable static.

So, if you want own baseUrl for every objects, then you should not declare it as static.

class Sample1page extends BaseTest {
    public String baseUrl;
    @Test
    public void method() throws InterruptedException {

        baseUrl = "http://example.com";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM