Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

[TechCorner Challenge #8] Finding Elements on a Web Page

Hi TestComplete Community!

 

Today, I bring you a new TechCorner Challenge task πŸ™‚

 

By completing the tasks, you will not only learn TestComplete features but also get into the TechCorner Leaderboard.

 

Today we will use the Leaderboard table itself to sharpen your skills! Let’s use TestComplete to find out if a user is on the table and how many points they gained so far!

Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.
Can you compete with people in the Leaderboard?😊

Difficulty:

Tip: You can use FindChild or FindElement in your script.

Note: You will need a Web Module to fulfill this task.

 

Good luck!

  • Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    'The following Vb Script code can find the solution for Tech Corner Challenge #8.

     

    Sub LeaderBoardSearchDetails()
    
    Url = "https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075#M38161*"
    Call Browsers.Item(btChrome).Run(Url)
    
    set Page = Sys.Browser("chrome").Page("*")
    
    set TechCornerChallenge_BoardText = Page.FindChild("contentText", "Techcorner challenge Leaderbord", 1000)
    
    Sys.HighlightObject TechCornerChallenge_BoardText,2
    
    TechCornerChallenge_BoardTable = Page.FindAll("className", "techcornerleaderboard", 1000)
    
    'Sys.HighlightObject TechCornerChallenge_BoardTable(0),2
    
    ColumnName = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr[1]/th", 1000)
    NoOfRows = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr", 1000)
    If UBound(ColumnName)>0 And UBound(NoOfRows)>0 Then
    For i= LBound(ColumnName) to UBound(ColumnName)
    If ColumnName(i).contentText = "Participant" Then
    LeaderBoardParticipants = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr/td[2]", 1000)
    Points = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr/td[2]/following-sibling::td", 1000)
    For j=LBound(LeaderBoardParticipants) to UBound(LeaderBoardParticipants)
    ParticipantUser1 = "@mkambham"
    ParticipantUser2 = "@tristaanogre"
    
    
    If LeaderBoardParticipants(j).contentText = ParticipantUser1 Then
    User1Details = "Participant Id: "+LeaderBoardParticipants(j).contentText & " and Points: "+Points(j).contentText
    Log.Message User1Details
    End If
    If LeaderBoardParticipants(j).contentText = ParticipantUser2 Then
    UserDetails2 = "Participant Id: "+LeaderBoardParticipants(j).contentText & " and Points: "+Points(j).contentText
    Log.Message UserDetails2
    End If
    Next
    
    
    For j=LBound(LeaderBoardParticipants) to UBound(LeaderBoardParticipants)
    ParticipantWithDetails = "Participant Id: " & LeaderBoardParticipants(j).contentText & " and Points: "& Points(j).contentText & VbNewLine
    AllParticipantsWithDetails = AllParticipantsWithDetails + ParticipantWithDetails
    Next
    Log.Message "AllParticipants Details in Single Log..."
    Log.Message AllParticipantsWithDetails
    
    End If
    Next
    End If
    
    End Sub

     

     

  • Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    //JavaScript
    
    function test1(){
    Browsers.Item(btChrome).Run("https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075")
    var page = Sys.Browser("chrome").Page("*");
    let user1 = page.FindChildByXPath("//td/a[contains(text(),'prekar')]");
    let user2 = page.FindChildByXPath("//td/a[contains(text(),'tristaanogre')]");
    if ((user1 != null)||(user2 != null)){
    if (user1 != null){
    let point1 = page.FindChildByXPath("//td/a[contains(text(),'@tristaanogre')]/following::td[1]");
    Log.Message("User prekar points " + point1.textContent);
    }
    if(user2 != null){
    let point2 = page.FindChildByXPath("//td/a[contains(text(),'@tristaanogre')]/following::td[1]");
    Log.Message("User tristaanogre points " + point2.textContent);
    }
    }
    else
    Log.Message("Both the user not found")
    }
  • Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    //JScript
    
    function PostTheScore()
    {
    
    //Open the user leaderboard page
    Browsers.Item(btChrome).Navigate("https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075#M38161");
    
    var Page = Sys.Browser().Page("*");
    
    //identify the leaderboard table
    
    var table = Page.QuerySelector('table.techcornerleaderboard');
    
    if (table.Exists)
    {
    // get the username of the connected session
    var userIcon = Page.QuerySelector("div.lia-quilt-row.lia-quilt-row-main-header img.lia-user-avatar-message").Click();
    Page.Wait();
    var myUserName = Page.QuerySelector('div.UserName a.lia-link-navigation.lia-page-link.lia-user-name-link');
    Page.QuerySelector("div.lia-quilt-row.lia-quilt-row-main-header img.lia-user-avatar-message").Click();
    
    
    if (myUserName.Exists)
    {
    // Search for my user (connected user) in the table
    props = ["ObjectType","contentText"];
    values = ["Cell", "@"+myUserName.ContentText];
    Mycell = table.FindChild(props, values, 20);
    
    if(Mycell.Exists)
    {
    // Display the score
    Log.Message("The score of my user "+ myUserName.ContentText+" is: "+table.cell(Mycell.RowIndex,2).innerText);
    }
    else
    {
    Log.Warning("My user is not found in the table of leaderboard");
    }
    
    }
    else
    {
    Log.Warning("No user is connected");
    }
    
    //Post the score of the participant tristaanogre
    // Search for the user in the table
    props = ["ObjectType","contentText"];
    values = ["Cell", "@tristaanogre"];
    CellP = table.FindChild(props, values, 20);
    
    if(CellP.Exists)
    {
    
    // Display the score
    Log.Message("The score of the user tristaanogre is: " +table.cell(CellP.RowIndex,2).innerText);
    }
    else
    {
    Log.Warning("The user tristaanogre is not found in the table of leaderboard");
    }
    
    }
    else
    Log.Warning("The table was not found");
    
    }

     

     

  • SiwarSayahi's avatar
    SiwarSayahi
    Occasional Contributor

    Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    //JScript
    
    function PostTheScore()
    {
    
    //Open the user leaderboard page
    Browsers.Item(btChrome).Navigate("https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075#M38161");
    
    var Page = Sys.Browser().Page("*");
    
    //identify the leaderboard table
    
    var table = Page.QuerySelector('table.techcornerleaderboard');
    
    if (table.Exists)
    {
    // get the username of the connected session
    var userIcon = Page.QuerySelector("div.lia-quilt-row.lia-quilt-row-main-header img.lia-user-avatar-message").Click();
    Page.Wait();
    var myUserName = Page.QuerySelector('div.UserName a.lia-link-navigation.lia-page-link.lia-user-name-link');
    Page.QuerySelector("div.lia-quilt-row.lia-quilt-row-main-header img.lia-user-avatar-message").Click();
    
    
    if (myUserName.Exists)
    {
    // Search for my user (connected user) in the table
    props = ["ObjectType","contentText"];
    values = ["Cell", "@"+myUserName.ContentText];
    Mycell = table.FindChild(props, values, 20);
    
    if(Mycell.Exists)
    {
    // Display the score
    Log.Message("The score of my user "+ myUserName.ContentText+" is: "+table.cell(Mycell.RowIndex,2).innerText);
    }
    else
    {
    Log.Warning("My user is not found in the table of leaderboard");
    }
    
    }
    else
    {
    Log.Warning("No user is connected");
    }
    
    //Post the score of the participant tristaanogre
    // Search for the user in the table
    props = ["ObjectType","contentText"];
    values = ["Cell", "@tristaanogre"];
    CellP = table.FindChild(props, values, 20);
    
    if(CellP.Exists)
    {
    
    // Display the score
    Log.Message("The score of the user tristaanogre is: " +table.cell(CellP.RowIndex,2).innerText);
    }
    else
    {
    Log.Warning("The user tristaanogre is not found in the table of leaderboard");
    }
    
    }
    else
    Log.Warning("The table was not found");
    
    }

     

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      mkambham Thank you for your script! Posting all the leaderboard scores to the log is a nice touch!

       

      prekar Great job! A short and sweet script!

       

      SiwarSayahi Thank you! Dynamically obtaining the name of the current user is a very interesting solution.

       

      A message to all participants - we have a great feature that can help you make your code look even better and more readable - expand the toolbar when writing a new comment and use Insert/Edit code sample to insert your code. You can choose the language there, too, for the correct syntax highlighting!

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Wow... I'm honored that I'm a subject of a techcorner challenge. πŸ˜‰

      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Hi tristaanogre !

        You are a celebrity on our forums, it was only natural to use your nickname πŸ˜Š

  • prekar's avatar
    prekar
    Occasional Contributor

    Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    //JavaScript
    
    function test1(){
    Browsers.Item(btChrome).Run("https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075")
    var page = Sys.Browser("chrome").Page("*");
    let user1 = page.FindChildByXPath("//td/a[contains(text(),'prekar')]");
    let user2 = page.FindChildByXPath("//td/a[contains(text(),'tristaanogre')]");
    if ((user1 != null)||(user2 != null)){
    if (user1 != null){
    let point1 = page.FindChildByXPath("//td/a[contains(text(),'@tristaanogre')]/following::td[1]");
    Log.Message("User prekar points " + point1.textContent);
    }
    if(user2 != null){
    let point2 = page.FindChildByXPath("//td/a[contains(text(),'@tristaanogre')]/following::td[1]");
    Log.Message("User tristaanogre points " + point2.textContent);
    }
    }
    else
    Log.Message("Both the user not found")
    }
  • mkambham's avatar
    mkambham
    Occasional Contributor

    Task: Create a TestComplete script that will go to the Leaderboard page, search for yourself (by your user name) and a user named tristaanogre in the table and, if this user is mentioned on the Leaderboard, post their score to the log.

     

    This is a solution created for [TechCorner Challenge #8]

     

    'The following Vb Script code can find the solution for Tech Corner Challenge #8.

     

    Sub LeaderBoardSearchDetails()
    
    Url = "https://community.smartbear.com/t5/TestComplete-General-Discussions/Leaderboard-and-Guide-to-Weekly-TestComplete-TechCorner/m-p/205075#M38161*"
    Call Browsers.Item(btChrome).Run(Url)
    
    set Page = Sys.Browser("chrome").Page("*")
    
    set TechCornerChallenge_BoardText = Page.FindChild("contentText", "Techcorner challenge Leaderbord", 1000)
    
    Sys.HighlightObject TechCornerChallenge_BoardText,2
    
    TechCornerChallenge_BoardTable = Page.FindAll("className", "techcornerleaderboard", 1000)
    
    'Sys.HighlightObject TechCornerChallenge_BoardTable(0),2
    
    ColumnName = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr[1]/th", 1000)
    NoOfRows = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr", 1000)
    If UBound(ColumnName)>0 And UBound(NoOfRows)>0 Then
    For i= LBound(ColumnName) to UBound(ColumnName)
    If ColumnName(i).contentText = "Participant" Then
    LeaderBoardParticipants = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr/td[2]", 1000)
    Points = Page.EvaluateXPath("//*[@id='bodyDisplay']/div/table/tbody/tr/td[2]/following-sibling::td", 1000)
    For j=LBound(LeaderBoardParticipants) to UBound(LeaderBoardParticipants)
    ParticipantUser1 = "@mkambham"
    ParticipantUser2 = "@tristaanogre"
    
    
    If LeaderBoardParticipants(j).contentText = ParticipantUser1 Then
    User1Details = "Participant Id: "+LeaderBoardParticipants(j).contentText & " and Points: "+Points(j).contentText
    Log.Message User1Details
    End If
    If LeaderBoardParticipants(j).contentText = ParticipantUser2 Then
    UserDetails2 = "Participant Id: "+LeaderBoardParticipants(j).contentText & " and Points: "+Points(j).contentText
    Log.Message UserDetails2
    End If
    Next
    
    
    For j=LBound(LeaderBoardParticipants) to UBound(LeaderBoardParticipants)
    ParticipantWithDetails = "Participant Id: " & LeaderBoardParticipants(j).contentText & " and Points: "& Points(j).contentText & VbNewLine
    AllParticipantsWithDetails = AllParticipantsWithDetails + ParticipantWithDetails
    Next
    Log.Message "AllParticipants Details in Single Log..."
    Log.Message AllParticipantsWithDetails
    
    End If
    Next
    End If
    
    End Sub