how to write script for same procedure for all same class element?












0















I am new in selenium webdriver with java. I have <a class="row-title"> property up to 21 web element.I want to do code in loop so i can merge java selenium code in to small, currently i have to write same script for all 21 test. Ex.



<a class="row-title">1</a>>
........
........
<a class="row-tilte">21</a>.

also i have to do same clicking and update procedure up to 1 to 21


package Dev_admin;
import java.nio.channels.SelectableChannel;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
public class present extends login{
@Test(priority = 1)
public void update1() {
driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}
@Test(priority = 2)
public void update2(){
driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();


//if i am using class name instead of x path , then how to do same procedure
in loop so my code became so small or merged..

@Test(priority = 1)
public void update1() {
driver.findElement(By.className("row-title")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}

@Test(priority = 2)
public void update2(){
driver.findElement(By.className("row-title")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
[@id='menu-posts-presentation']/a/div[3]")));
element.click();
}









share|improve this question





























    0















    I am new in selenium webdriver with java. I have <a class="row-title"> property up to 21 web element.I want to do code in loop so i can merge java selenium code in to small, currently i have to write same script for all 21 test. Ex.



    <a class="row-title">1</a>>
    ........
    ........
    <a class="row-tilte">21</a>.

    also i have to do same clicking and update procedure up to 1 to 21


    package Dev_admin;
    import java.nio.channels.SelectableChannel;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebElement;
    public class present extends login{
    @Test(priority = 1)
    public void update1() {
    driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
    element.click();
    }
    @Test(priority = 2)
    public void update2(){
    driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element =
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
    [@id='menu-posts-presentation']/a/div[3]")));
    element.click();


    //if i am using class name instead of x path , then how to do same procedure
    in loop so my code became so small or merged..

    @Test(priority = 1)
    public void update1() {
    driver.findElement(By.className("row-title")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element =
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
    [@id='menu-posts-presentation']/a/div[3]")));
    element.click();
    }

    @Test(priority = 2)
    public void update2(){
    driver.findElement(By.className("row-title")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element =
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
    [@id='menu-posts-presentation']/a/div[3]")));
    element.click();
    }









    share|improve this question



























      0












      0








      0








      I am new in selenium webdriver with java. I have <a class="row-title"> property up to 21 web element.I want to do code in loop so i can merge java selenium code in to small, currently i have to write same script for all 21 test. Ex.



      <a class="row-title">1</a>>
      ........
      ........
      <a class="row-tilte">21</a>.

      also i have to do same clicking and update procedure up to 1 to 21


      package Dev_admin;
      import java.nio.channels.SelectableChannel;
      import java.util.concurrent.TimeUnit;
      import org.openqa.selenium.By;
      import org.openqa.selenium.JavascriptExecutor;
      import org.openqa.selenium.Keys;
      import org.openqa.selenium.WebElement;
      public class present extends login{
      @Test(priority = 1)
      public void update1() {
      driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }
      @Test(priority = 2)
      public void update2(){
      driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();


      //if i am using class name instead of x path , then how to do same procedure
      in loop so my code became so small or merged..

      @Test(priority = 1)
      public void update1() {
      driver.findElement(By.className("row-title")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }

      @Test(priority = 2)
      public void update2(){
      driver.findElement(By.className("row-title")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }









      share|improve this question
















      I am new in selenium webdriver with java. I have <a class="row-title"> property up to 21 web element.I want to do code in loop so i can merge java selenium code in to small, currently i have to write same script for all 21 test. Ex.



      <a class="row-title">1</a>>
      ........
      ........
      <a class="row-tilte">21</a>.

      also i have to do same clicking and update procedure up to 1 to 21


      package Dev_admin;
      import java.nio.channels.SelectableChannel;
      import java.util.concurrent.TimeUnit;
      import org.openqa.selenium.By;
      import org.openqa.selenium.JavascriptExecutor;
      import org.openqa.selenium.Keys;
      import org.openqa.selenium.WebElement;
      public class present extends login{
      @Test(priority = 1)
      public void update1() {
      driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }
      @Test(priority = 2)
      public void update2(){
      driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();


      //if i am using class name instead of x path , then how to do same procedure
      in loop so my code became so small or merged..

      @Test(priority = 1)
      public void update1() {
      driver.findElement(By.className("row-title")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }

      @Test(priority = 2)
      public void update2(){
      driver.findElement(By.className("row-title")).click();
      WebDriverWait wait = new WebDriverWait(driver, 20);
      WebElement element =
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*
      [@id='menu-posts-presentation']/a/div[3]")));
      element.click();
      }






      java arrays selenium selenium-webdriver automation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 11:13









      Ishita Shah

      2,3001726




      2,3001726










      asked Nov 23 '18 at 10:50









      Fijan KajiFijan Kaji

      103




      103
























          2 Answers
          2






          active

          oldest

          votes


















          0














          @Test
          public void test() {
          WebDriverWait wait = new WebDriverWait(driver, 20);
          //Finding all elements and saving to the list (we expect list to have 21 element)
          List<WebElement> rowEls = driver.findElements(By.className("row-title"));
          //Looping through each of 21 element and doing necessary actions for every element inside the loop
          for (WebElement rowEl: rowEls) {
          rowEl.click();
          WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
          element.click();
          }
          }


          Side note: Don't forget to add at least one assertion to your test. Currently it will just to some clicks without verifying anything.






          share|improve this answer
























          • now i am able to fetch all class name.

            – Fijan Kaji
            Dec 19 '18 at 6:39





















          0














          Modified wait method:



          public WebDriverWait wait_sec(WebDriver driver, int sec) {
          return new WebDriverWait(driver, sec);
          }


          Managing WebElements:



          public WebElement get_element_by_classname (WebDriver driver, String classname) {
          WebElement element = driver.findElement(By.className(classname));
          return element;
          }

          public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
          WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
          return element;
          }


          Usage:



          @Test
          public void update_x() {
          get_element_by_classname(driver, "row-title").click();
          wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
          }





          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%2f53445262%2fhow-to-write-script-for-same-procedure-for-all-same-class-element%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            @Test
            public void test() {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            //Finding all elements and saving to the list (we expect list to have 21 element)
            List<WebElement> rowEls = driver.findElements(By.className("row-title"));
            //Looping through each of 21 element and doing necessary actions for every element inside the loop
            for (WebElement rowEl: rowEls) {
            rowEl.click();
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
            element.click();
            }
            }


            Side note: Don't forget to add at least one assertion to your test. Currently it will just to some clicks without verifying anything.






            share|improve this answer
























            • now i am able to fetch all class name.

              – Fijan Kaji
              Dec 19 '18 at 6:39


















            0














            @Test
            public void test() {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            //Finding all elements and saving to the list (we expect list to have 21 element)
            List<WebElement> rowEls = driver.findElements(By.className("row-title"));
            //Looping through each of 21 element and doing necessary actions for every element inside the loop
            for (WebElement rowEl: rowEls) {
            rowEl.click();
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
            element.click();
            }
            }


            Side note: Don't forget to add at least one assertion to your test. Currently it will just to some clicks without verifying anything.






            share|improve this answer
























            • now i am able to fetch all class name.

              – Fijan Kaji
              Dec 19 '18 at 6:39
















            0












            0








            0







            @Test
            public void test() {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            //Finding all elements and saving to the list (we expect list to have 21 element)
            List<WebElement> rowEls = driver.findElements(By.className("row-title"));
            //Looping through each of 21 element and doing necessary actions for every element inside the loop
            for (WebElement rowEl: rowEls) {
            rowEl.click();
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
            element.click();
            }
            }


            Side note: Don't forget to add at least one assertion to your test. Currently it will just to some clicks without verifying anything.






            share|improve this answer













            @Test
            public void test() {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            //Finding all elements and saving to the list (we expect list to have 21 element)
            List<WebElement> rowEls = driver.findElements(By.className("row-title"));
            //Looping through each of 21 element and doing necessary actions for every element inside the loop
            for (WebElement rowEl: rowEls) {
            rowEl.click();
            WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
            element.click();
            }
            }


            Side note: Don't forget to add at least one assertion to your test. Currently it will just to some clicks without verifying anything.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 6:08









            Vladimir EfimovVladimir Efimov

            695311




            695311













            • now i am able to fetch all class name.

              – Fijan Kaji
              Dec 19 '18 at 6:39





















            • now i am able to fetch all class name.

              – Fijan Kaji
              Dec 19 '18 at 6:39



















            now i am able to fetch all class name.

            – Fijan Kaji
            Dec 19 '18 at 6:39







            now i am able to fetch all class name.

            – Fijan Kaji
            Dec 19 '18 at 6:39















            0














            Modified wait method:



            public WebDriverWait wait_sec(WebDriver driver, int sec) {
            return new WebDriverWait(driver, sec);
            }


            Managing WebElements:



            public WebElement get_element_by_classname (WebDriver driver, String classname) {
            WebElement element = driver.findElement(By.className(classname));
            return element;
            }

            public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
            WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
            return element;
            }


            Usage:



            @Test
            public void update_x() {
            get_element_by_classname(driver, "row-title").click();
            wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
            }





            share|improve this answer




























              0














              Modified wait method:



              public WebDriverWait wait_sec(WebDriver driver, int sec) {
              return new WebDriverWait(driver, sec);
              }


              Managing WebElements:



              public WebElement get_element_by_classname (WebDriver driver, String classname) {
              WebElement element = driver.findElement(By.className(classname));
              return element;
              }

              public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
              WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
              return element;
              }


              Usage:



              @Test
              public void update_x() {
              get_element_by_classname(driver, "row-title").click();
              wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
              }





              share|improve this answer


























                0












                0








                0







                Modified wait method:



                public WebDriverWait wait_sec(WebDriver driver, int sec) {
                return new WebDriverWait(driver, sec);
                }


                Managing WebElements:



                public WebElement get_element_by_classname (WebDriver driver, String classname) {
                WebElement element = driver.findElement(By.className(classname));
                return element;
                }

                public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
                WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
                return element;
                }


                Usage:



                @Test
                public void update_x() {
                get_element_by_classname(driver, "row-title").click();
                wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
                }





                share|improve this answer













                Modified wait method:



                public WebDriverWait wait_sec(WebDriver driver, int sec) {
                return new WebDriverWait(driver, sec);
                }


                Managing WebElements:



                public WebElement get_element_by_classname (WebDriver driver, String classname) {
                WebElement element = driver.findElement(By.className(classname));
                return element;
                }

                public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
                WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
                return element;
                }


                Usage:



                @Test
                public void update_x() {
                get_element_by_classname(driver, "row-title").click();
                wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 13:53









                pburgrpburgr

                449129




                449129






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445262%2fhow-to-write-script-for-same-procedure-for-all-same-class-element%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...