Reputation: 1414
I've been asked to create a test which can cycle through all the links on a page with webdriver in C#.
I really don't where to begin with this, so sorry for not having any code to provide.
I'll need to somehow select all the links on the page and place them into an array or the like and them simply loop through them, click link, back to previous page, click next link etc.
Upvotes: 2
Views: 5942
Reputation: 793
Please look here: How to browse a whole website using selenium?
Since you mentioned one page and not the whole site there are some straightforward ways.
populate an implementation of IWebElements
with an xpath locator for //a
...or get it by tagname as Margus mentions.
Then loop through this and click each IWebElement
in the IWebElements
list. Save the driver.Url
in a list of items for review or output later with whatever else you want to capture from the page. You can validate here if you have known parameters to check for. Then execute a driver.Navigate().Back()
. This will put you back in the previous spot to test the next element. Might also want to save the href as well as Margus suggests and compare that with the driver.Url
on the other page.
Update based on comment
public class Links
{
public string LinkName { get; set; }
public string href { get; set; }
public string newPageUrl { get; set; }
}
string newPageUrl = "";
string linkName = "";
string href = "";
List<Links> links = new List<Links>();
foreach (IWebElement item in _driver.FindElements(By.TagName("a")))
{
href = item.GetAttribute("href");
linkName = item.text();
item.click();
newPageUrl = driver.Url();
links.Add(new Links
{
NewPageUrl = newPageUrl,
Href = href,
LinkName = linkName
});
driver.Navigate().Back();
}
Then your List will be a list of that class which will have the specific properties for each link you want as well as the navigation with the new url. Then you can easily add something to this if you want to keep track of something else or test something else.
Upvotes: 2
Reputation: 20048
I assume it is something in the lines of:
foreach (var item in _driver.FindElements(By.TagName("a")))
{
Trace.WriteLine(item.GetAttribute("href"));
}
Note this is written from head and approximate solution using NUnit.
[TestFixture]
class UnitTests
{
[TestFixtureSetUp]
public void FixtureSetup()
{
_driver = new ChromeDriver();
_driver.Manage().Timeouts().ImplicitlyWait(Defaultamount);
_wait = new WebDriverWait(_driver, Defaultamount);
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
_driver.Quit();
}
//...
}
Code would be something in the lines of :
[TestCase("www.googel.com")]
public bool TestAllWebpageLinks(string url, Result = true)
{
_driver.Navigate().GoToUrl(url);
var result = _driver.FindElements(By.TagName("a"))
.Select(o=>o.GetAttribute("href"))
.ToDictionary(o=>o,o=>TestPage(url,o));
return result.All(o => o.Value);
}
public bool TestPage(string url, string link){
try
{
_driver.Navigate().GoToUrl(url);
_driver.FindElement(By.XPath("//a[@href='"+link+"']")).Click();
return true;
}
catch (Exception ex)
{
return false;
}
}
Just as a note, Resharper plugin will simplify running unit tests a lot.
Upvotes: 3
Reputation: 1484
This complete program might help you: The code is in Java
I am printing all the link Name.
import java.util.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TrailRuns
{
public static void main(String[] args) throws Exception
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.googl.com/");
driver.manage().window().maximize();
List<WebElement> objWEs = driver.findElements(By.tagName("a"));
for(WebElement ele:objWEs)
System.out.println(ele.getText());
}
}
Upvotes: 0
Reputation: 228
With Selenium-WebDriver you can simply call:
Or if you want more options you could use a library like http://sizzlejs.com/ with webdriver which would let you call something like:
You could also use other external helpers like:
It depends on your page and how the links are generated.
Upvotes: 0