How to pass “By” and String for it in Selenium test method?












0














So I want to pass two things for my methods, String - that would be a property name and By - that would allow to chose by what criteria element should be searched. Simple example:



public static WebElement getElement(By by) {
return driver.findElement(by);
}


But that would force me to use it that way:



element = getElement(By.className(properties.getProperty("class")));


While I would like to use it like this:



element = getElement(By.className, "class");


How can I do this? I thought that simple code like this would work, but unfortunately it returns error




"by(String) is undefined"




public static WebElement getElement(By by, String string) {
return driver.findElement(by(properties.getProperty(string));
}


EDIT:
I decided to settle on using:



public static String useProperty(String propertyName) {
return properties.getProperty(propertyName);
}


Not exactly how I wanted to handle it, but it does allow to simplify and improve readability of code.










share|improve this question





























    0














    So I want to pass two things for my methods, String - that would be a property name and By - that would allow to chose by what criteria element should be searched. Simple example:



    public static WebElement getElement(By by) {
    return driver.findElement(by);
    }


    But that would force me to use it that way:



    element = getElement(By.className(properties.getProperty("class")));


    While I would like to use it like this:



    element = getElement(By.className, "class");


    How can I do this? I thought that simple code like this would work, but unfortunately it returns error




    "by(String) is undefined"




    public static WebElement getElement(By by, String string) {
    return driver.findElement(by(properties.getProperty(string));
    }


    EDIT:
    I decided to settle on using:



    public static String useProperty(String propertyName) {
    return properties.getProperty(propertyName);
    }


    Not exactly how I wanted to handle it, but it does allow to simplify and improve readability of code.










    share|improve this question



























      0












      0








      0







      So I want to pass two things for my methods, String - that would be a property name and By - that would allow to chose by what criteria element should be searched. Simple example:



      public static WebElement getElement(By by) {
      return driver.findElement(by);
      }


      But that would force me to use it that way:



      element = getElement(By.className(properties.getProperty("class")));


      While I would like to use it like this:



      element = getElement(By.className, "class");


      How can I do this? I thought that simple code like this would work, but unfortunately it returns error




      "by(String) is undefined"




      public static WebElement getElement(By by, String string) {
      return driver.findElement(by(properties.getProperty(string));
      }


      EDIT:
      I decided to settle on using:



      public static String useProperty(String propertyName) {
      return properties.getProperty(propertyName);
      }


      Not exactly how I wanted to handle it, but it does allow to simplify and improve readability of code.










      share|improve this question















      So I want to pass two things for my methods, String - that would be a property name and By - that would allow to chose by what criteria element should be searched. Simple example:



      public static WebElement getElement(By by) {
      return driver.findElement(by);
      }


      But that would force me to use it that way:



      element = getElement(By.className(properties.getProperty("class")));


      While I would like to use it like this:



      element = getElement(By.className, "class");


      How can I do this? I thought that simple code like this would work, but unfortunately it returns error




      "by(String) is undefined"




      public static WebElement getElement(By by, String string) {
      return driver.findElement(by(properties.getProperty(string));
      }


      EDIT:
      I decided to settle on using:



      public static String useProperty(String propertyName) {
      return properties.getProperty(propertyName);
      }


      Not exactly how I wanted to handle it, but it does allow to simplify and improve readability of code.







      java selenium selenium-webdriver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 10:02

























      asked Nov 21 '18 at 8:36









      LVN

      154




      154
























          4 Answers
          4






          active

          oldest

          votes


















          6














          This isn't possible, By.className() is a method which receives a String parameter. This String is used to initialize the inner class By.ByClassName and this is the returned By.



          You could build switch case to handle this, something like



          public static WebElement getElement(String string) {
          By by = null;
          String locator = properties.getProperty(string);

          switch (string) {
          case "class":
          by = By.className(locator);
          break;
          case "id":
          by = By.id(locator);
          break;
          }

          return driver.findElement(by);
          }





          share|improve this answer



















          • 1




            Very good answer!
            – Ratmir Asanov
            Nov 21 '18 at 9:54






          • 2




            @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
            – Guy
            Nov 21 '18 at 9:55






          • 2




            Yes, sorry, this is my fault.
            – Ratmir Asanov
            Nov 21 '18 at 9:56








          • 1




            Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
            – LVN
            Nov 21 '18 at 10:00





















          0














          The "By" can not be represented as String. There is another example:



          public void Test_xy() throws InterruptedException {     

          String by = {"id", "xpath"};
          String property_name = {"value", "title"};

          driver.get("https://www.example.com");
          Thread.sleep(5000);
          WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
          String element_title = getProperty(more_info, property_name[1]);

          }

          public WebElement getElement(String by, String by_value) {
          if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
          else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
          // else if (by == different locator) ...
          else {return null;}
          }

          public String getProperty(WebElement element, String chosen_property) {
          return element.getAttribute(chosen_property);
          }





          share|improve this answer





























            0














            One approach could be with generics:



            private <T extends By> WebElement getElement(Class<T> byType, String prop) {

            if (byType.equals(ByClassName.class))
            return getDriver().findElement(By.className(prop));
            else if (byType.equals(ByCssSelector.class))
            return getDriver().findElement(By.cssSelector(prop));
            else if (byType.equals(ById.class))
            return getDriver().findElement(By.id(prop));
            else if (byType.equals(ByLinkText.class))
            return getDriver().findElement(By.linkText(prop));
            else if (byType.equals(ByName.class))
            return getDriver().findElement(By.name(prop));
            else if (byType.equals(ByPartialLinkText.class))
            return getDriver().findElement(By.partialLinkText(prop));
            else if (byType.equals(ByTagName.class))
            return getDriver().findElement(By.tagName(prop));
            else if (byType.equals(ByXPath.class))
            return getDriver().findElement(By.xpath(prop));
            else
            throw new UnsupportedOperationException();
            }


            And you would use it like:



            @Test
            public void myWebElementTest() {
            WebElement div1 = getElement(By.ByName.class, "div1");
            WebElement id1 = getElement(By.ById.class, "id1");
            }





            share|improve this answer





























              0














              Simple Implementation :



              public WebElement getLocator(String locatorType, String locatorValue ){

              switch(locatorType.toUpperCase()) {
              case "ID":
              return driver.findElement(By.id(locatorValue ));
              case "CSSSELECTOR":
              return driver.findElement(By.cssSelector(locatorValue));
              case "XPATH":
              return driver.findElement(By.xpath(locatorValue));
              case "NAME":
              return driver.findElement(By.name(locatorValue));
              case "LINKTEXT":
              return driver.findElement(By.linkText(locatorValue));
              case "PARTIALLINKTEXT":
              return driver.findElement(By.partialLinkText(locatorValue);
              case "TAGNAME":
              return driver.findElement(By.tagName(locatorValue));
              case "CLASSNAME":
              return driver.findElement(By.className(locatorValue));
              default:
              LOGGER.info("Incorrect locator or Type" + locatorType);
              }
              }





              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408054%2fhow-to-pass-by-and-string-for-it-in-selenium-test-method%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                6














                This isn't possible, By.className() is a method which receives a String parameter. This String is used to initialize the inner class By.ByClassName and this is the returned By.



                You could build switch case to handle this, something like



                public static WebElement getElement(String string) {
                By by = null;
                String locator = properties.getProperty(string);

                switch (string) {
                case "class":
                by = By.className(locator);
                break;
                case "id":
                by = By.id(locator);
                break;
                }

                return driver.findElement(by);
                }





                share|improve this answer



















                • 1




                  Very good answer!
                  – Ratmir Asanov
                  Nov 21 '18 at 9:54






                • 2




                  @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                  – Guy
                  Nov 21 '18 at 9:55






                • 2




                  Yes, sorry, this is my fault.
                  – Ratmir Asanov
                  Nov 21 '18 at 9:56








                • 1




                  Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                  – LVN
                  Nov 21 '18 at 10:00


















                6














                This isn't possible, By.className() is a method which receives a String parameter. This String is used to initialize the inner class By.ByClassName and this is the returned By.



                You could build switch case to handle this, something like



                public static WebElement getElement(String string) {
                By by = null;
                String locator = properties.getProperty(string);

                switch (string) {
                case "class":
                by = By.className(locator);
                break;
                case "id":
                by = By.id(locator);
                break;
                }

                return driver.findElement(by);
                }





                share|improve this answer



















                • 1




                  Very good answer!
                  – Ratmir Asanov
                  Nov 21 '18 at 9:54






                • 2




                  @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                  – Guy
                  Nov 21 '18 at 9:55






                • 2




                  Yes, sorry, this is my fault.
                  – Ratmir Asanov
                  Nov 21 '18 at 9:56








                • 1




                  Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                  – LVN
                  Nov 21 '18 at 10:00
















                6












                6








                6






                This isn't possible, By.className() is a method which receives a String parameter. This String is used to initialize the inner class By.ByClassName and this is the returned By.



                You could build switch case to handle this, something like



                public static WebElement getElement(String string) {
                By by = null;
                String locator = properties.getProperty(string);

                switch (string) {
                case "class":
                by = By.className(locator);
                break;
                case "id":
                by = By.id(locator);
                break;
                }

                return driver.findElement(by);
                }





                share|improve this answer














                This isn't possible, By.className() is a method which receives a String parameter. This String is used to initialize the inner class By.ByClassName and this is the returned By.



                You could build switch case to handle this, something like



                public static WebElement getElement(String string) {
                By by = null;
                String locator = properties.getProperty(string);

                switch (string) {
                case "class":
                by = By.className(locator);
                break;
                case "id":
                by = By.id(locator);
                break;
                }

                return driver.findElement(by);
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 9:52

























                answered Nov 21 '18 at 9:47









                Guy

                18.3k62149




                18.3k62149








                • 1




                  Very good answer!
                  – Ratmir Asanov
                  Nov 21 '18 at 9:54






                • 2




                  @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                  – Guy
                  Nov 21 '18 at 9:55






                • 2




                  Yes, sorry, this is my fault.
                  – Ratmir Asanov
                  Nov 21 '18 at 9:56








                • 1




                  Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                  – LVN
                  Nov 21 '18 at 10:00
















                • 1




                  Very good answer!
                  – Ratmir Asanov
                  Nov 21 '18 at 9:54






                • 2




                  @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                  – Guy
                  Nov 21 '18 at 9:55






                • 2




                  Yes, sorry, this is my fault.
                  – Ratmir Asanov
                  Nov 21 '18 at 9:56








                • 1




                  Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                  – LVN
                  Nov 21 '18 at 10:00










                1




                1




                Very good answer!
                – Ratmir Asanov
                Nov 21 '18 at 9:54




                Very good answer!
                – Ratmir Asanov
                Nov 21 '18 at 9:54




                2




                2




                @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                – Guy
                Nov 21 '18 at 9:55




                @RatmirAsanov Thanks. I saw the edit, the switch need to be on the string, not the locator.
                – Guy
                Nov 21 '18 at 9:55




                2




                2




                Yes, sorry, this is my fault.
                – Ratmir Asanov
                Nov 21 '18 at 9:56






                Yes, sorry, this is my fault.
                – Ratmir Asanov
                Nov 21 '18 at 9:56






                1




                1




                Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                – LVN
                Nov 21 '18 at 10:00






                Thanks, it is a solution, but I guess it's not very optimal way to handle it. I think I will settle on making additional method to shorten it.
                – LVN
                Nov 21 '18 at 10:00















                0














                The "By" can not be represented as String. There is another example:



                public void Test_xy() throws InterruptedException {     

                String by = {"id", "xpath"};
                String property_name = {"value", "title"};

                driver.get("https://www.example.com");
                Thread.sleep(5000);
                WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
                String element_title = getProperty(more_info, property_name[1]);

                }

                public WebElement getElement(String by, String by_value) {
                if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
                else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
                // else if (by == different locator) ...
                else {return null;}
                }

                public String getProperty(WebElement element, String chosen_property) {
                return element.getAttribute(chosen_property);
                }





                share|improve this answer


























                  0














                  The "By" can not be represented as String. There is another example:



                  public void Test_xy() throws InterruptedException {     

                  String by = {"id", "xpath"};
                  String property_name = {"value", "title"};

                  driver.get("https://www.example.com");
                  Thread.sleep(5000);
                  WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
                  String element_title = getProperty(more_info, property_name[1]);

                  }

                  public WebElement getElement(String by, String by_value) {
                  if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
                  else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
                  // else if (by == different locator) ...
                  else {return null;}
                  }

                  public String getProperty(WebElement element, String chosen_property) {
                  return element.getAttribute(chosen_property);
                  }





                  share|improve this answer
























                    0












                    0








                    0






                    The "By" can not be represented as String. There is another example:



                    public void Test_xy() throws InterruptedException {     

                    String by = {"id", "xpath"};
                    String property_name = {"value", "title"};

                    driver.get("https://www.example.com");
                    Thread.sleep(5000);
                    WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
                    String element_title = getProperty(more_info, property_name[1]);

                    }

                    public WebElement getElement(String by, String by_value) {
                    if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
                    else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
                    // else if (by == different locator) ...
                    else {return null;}
                    }

                    public String getProperty(WebElement element, String chosen_property) {
                    return element.getAttribute(chosen_property);
                    }





                    share|improve this answer












                    The "By" can not be represented as String. There is another example:



                    public void Test_xy() throws InterruptedException {     

                    String by = {"id", "xpath"};
                    String property_name = {"value", "title"};

                    driver.get("https://www.example.com");
                    Thread.sleep(5000);
                    WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
                    String element_title = getProperty(more_info, property_name[1]);

                    }

                    public WebElement getElement(String by, String by_value) {
                    if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
                    else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
                    // else if (by == different locator) ...
                    else {return null;}
                    }

                    public String getProperty(WebElement element, String chosen_property) {
                    return element.getAttribute(chosen_property);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 9:51









                    pburgr

                    427129




                    427129























                        0














                        One approach could be with generics:



                        private <T extends By> WebElement getElement(Class<T> byType, String prop) {

                        if (byType.equals(ByClassName.class))
                        return getDriver().findElement(By.className(prop));
                        else if (byType.equals(ByCssSelector.class))
                        return getDriver().findElement(By.cssSelector(prop));
                        else if (byType.equals(ById.class))
                        return getDriver().findElement(By.id(prop));
                        else if (byType.equals(ByLinkText.class))
                        return getDriver().findElement(By.linkText(prop));
                        else if (byType.equals(ByName.class))
                        return getDriver().findElement(By.name(prop));
                        else if (byType.equals(ByPartialLinkText.class))
                        return getDriver().findElement(By.partialLinkText(prop));
                        else if (byType.equals(ByTagName.class))
                        return getDriver().findElement(By.tagName(prop));
                        else if (byType.equals(ByXPath.class))
                        return getDriver().findElement(By.xpath(prop));
                        else
                        throw new UnsupportedOperationException();
                        }


                        And you would use it like:



                        @Test
                        public void myWebElementTest() {
                        WebElement div1 = getElement(By.ByName.class, "div1");
                        WebElement id1 = getElement(By.ById.class, "id1");
                        }





                        share|improve this answer


























                          0














                          One approach could be with generics:



                          private <T extends By> WebElement getElement(Class<T> byType, String prop) {

                          if (byType.equals(ByClassName.class))
                          return getDriver().findElement(By.className(prop));
                          else if (byType.equals(ByCssSelector.class))
                          return getDriver().findElement(By.cssSelector(prop));
                          else if (byType.equals(ById.class))
                          return getDriver().findElement(By.id(prop));
                          else if (byType.equals(ByLinkText.class))
                          return getDriver().findElement(By.linkText(prop));
                          else if (byType.equals(ByName.class))
                          return getDriver().findElement(By.name(prop));
                          else if (byType.equals(ByPartialLinkText.class))
                          return getDriver().findElement(By.partialLinkText(prop));
                          else if (byType.equals(ByTagName.class))
                          return getDriver().findElement(By.tagName(prop));
                          else if (byType.equals(ByXPath.class))
                          return getDriver().findElement(By.xpath(prop));
                          else
                          throw new UnsupportedOperationException();
                          }


                          And you would use it like:



                          @Test
                          public void myWebElementTest() {
                          WebElement div1 = getElement(By.ByName.class, "div1");
                          WebElement id1 = getElement(By.ById.class, "id1");
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            One approach could be with generics:



                            private <T extends By> WebElement getElement(Class<T> byType, String prop) {

                            if (byType.equals(ByClassName.class))
                            return getDriver().findElement(By.className(prop));
                            else if (byType.equals(ByCssSelector.class))
                            return getDriver().findElement(By.cssSelector(prop));
                            else if (byType.equals(ById.class))
                            return getDriver().findElement(By.id(prop));
                            else if (byType.equals(ByLinkText.class))
                            return getDriver().findElement(By.linkText(prop));
                            else if (byType.equals(ByName.class))
                            return getDriver().findElement(By.name(prop));
                            else if (byType.equals(ByPartialLinkText.class))
                            return getDriver().findElement(By.partialLinkText(prop));
                            else if (byType.equals(ByTagName.class))
                            return getDriver().findElement(By.tagName(prop));
                            else if (byType.equals(ByXPath.class))
                            return getDriver().findElement(By.xpath(prop));
                            else
                            throw new UnsupportedOperationException();
                            }


                            And you would use it like:



                            @Test
                            public void myWebElementTest() {
                            WebElement div1 = getElement(By.ByName.class, "div1");
                            WebElement id1 = getElement(By.ById.class, "id1");
                            }





                            share|improve this answer












                            One approach could be with generics:



                            private <T extends By> WebElement getElement(Class<T> byType, String prop) {

                            if (byType.equals(ByClassName.class))
                            return getDriver().findElement(By.className(prop));
                            else if (byType.equals(ByCssSelector.class))
                            return getDriver().findElement(By.cssSelector(prop));
                            else if (byType.equals(ById.class))
                            return getDriver().findElement(By.id(prop));
                            else if (byType.equals(ByLinkText.class))
                            return getDriver().findElement(By.linkText(prop));
                            else if (byType.equals(ByName.class))
                            return getDriver().findElement(By.name(prop));
                            else if (byType.equals(ByPartialLinkText.class))
                            return getDriver().findElement(By.partialLinkText(prop));
                            else if (byType.equals(ByTagName.class))
                            return getDriver().findElement(By.tagName(prop));
                            else if (byType.equals(ByXPath.class))
                            return getDriver().findElement(By.xpath(prop));
                            else
                            throw new UnsupportedOperationException();
                            }


                            And you would use it like:



                            @Test
                            public void myWebElementTest() {
                            WebElement div1 = getElement(By.ByName.class, "div1");
                            WebElement id1 = getElement(By.ById.class, "id1");
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 10:20









                            Ivan Markovic

                            659414




                            659414























                                0














                                Simple Implementation :



                                public WebElement getLocator(String locatorType, String locatorValue ){

                                switch(locatorType.toUpperCase()) {
                                case "ID":
                                return driver.findElement(By.id(locatorValue ));
                                case "CSSSELECTOR":
                                return driver.findElement(By.cssSelector(locatorValue));
                                case "XPATH":
                                return driver.findElement(By.xpath(locatorValue));
                                case "NAME":
                                return driver.findElement(By.name(locatorValue));
                                case "LINKTEXT":
                                return driver.findElement(By.linkText(locatorValue));
                                case "PARTIALLINKTEXT":
                                return driver.findElement(By.partialLinkText(locatorValue);
                                case "TAGNAME":
                                return driver.findElement(By.tagName(locatorValue));
                                case "CLASSNAME":
                                return driver.findElement(By.className(locatorValue));
                                default:
                                LOGGER.info("Incorrect locator or Type" + locatorType);
                                }
                                }





                                share|improve this answer


























                                  0














                                  Simple Implementation :



                                  public WebElement getLocator(String locatorType, String locatorValue ){

                                  switch(locatorType.toUpperCase()) {
                                  case "ID":
                                  return driver.findElement(By.id(locatorValue ));
                                  case "CSSSELECTOR":
                                  return driver.findElement(By.cssSelector(locatorValue));
                                  case "XPATH":
                                  return driver.findElement(By.xpath(locatorValue));
                                  case "NAME":
                                  return driver.findElement(By.name(locatorValue));
                                  case "LINKTEXT":
                                  return driver.findElement(By.linkText(locatorValue));
                                  case "PARTIALLINKTEXT":
                                  return driver.findElement(By.partialLinkText(locatorValue);
                                  case "TAGNAME":
                                  return driver.findElement(By.tagName(locatorValue));
                                  case "CLASSNAME":
                                  return driver.findElement(By.className(locatorValue));
                                  default:
                                  LOGGER.info("Incorrect locator or Type" + locatorType);
                                  }
                                  }





                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    Simple Implementation :



                                    public WebElement getLocator(String locatorType, String locatorValue ){

                                    switch(locatorType.toUpperCase()) {
                                    case "ID":
                                    return driver.findElement(By.id(locatorValue ));
                                    case "CSSSELECTOR":
                                    return driver.findElement(By.cssSelector(locatorValue));
                                    case "XPATH":
                                    return driver.findElement(By.xpath(locatorValue));
                                    case "NAME":
                                    return driver.findElement(By.name(locatorValue));
                                    case "LINKTEXT":
                                    return driver.findElement(By.linkText(locatorValue));
                                    case "PARTIALLINKTEXT":
                                    return driver.findElement(By.partialLinkText(locatorValue);
                                    case "TAGNAME":
                                    return driver.findElement(By.tagName(locatorValue));
                                    case "CLASSNAME":
                                    return driver.findElement(By.className(locatorValue));
                                    default:
                                    LOGGER.info("Incorrect locator or Type" + locatorType);
                                    }
                                    }





                                    share|improve this answer












                                    Simple Implementation :



                                    public WebElement getLocator(String locatorType, String locatorValue ){

                                    switch(locatorType.toUpperCase()) {
                                    case "ID":
                                    return driver.findElement(By.id(locatorValue ));
                                    case "CSSSELECTOR":
                                    return driver.findElement(By.cssSelector(locatorValue));
                                    case "XPATH":
                                    return driver.findElement(By.xpath(locatorValue));
                                    case "NAME":
                                    return driver.findElement(By.name(locatorValue));
                                    case "LINKTEXT":
                                    return driver.findElement(By.linkText(locatorValue));
                                    case "PARTIALLINKTEXT":
                                    return driver.findElement(By.partialLinkText(locatorValue);
                                    case "TAGNAME":
                                    return driver.findElement(By.tagName(locatorValue));
                                    case "CLASSNAME":
                                    return driver.findElement(By.className(locatorValue));
                                    default:
                                    LOGGER.info("Incorrect locator or Type" + locatorType);
                                    }
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 21 '18 at 10:29









                                    kushal.8

                                    2,1992722




                                    2,1992722






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408054%2fhow-to-pass-by-and-string-for-it-in-selenium-test-method%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Wiesbaden

                                        Marschland

                                        Dieringhausen