I want to test the tab order of my input fields on my page and I thought I could make it look something like this:
#get the number of inputs on the page
$input = $this->elements($this->using('css selector')->value('div.wrapper input'));
for ($i=1; $i <= count($input); $i++)
{
#just to set focus on the field i know is the first
if($i == 1)
{
$this->byId('myFirstField')->value($i);
}
else
{
$this->keysSpecial('TAB');
$this->get current element in focus->value($i);
}
}
Then I can either sit there and see the numbers are written in the right order or fully automate it and assert that the value is correct for every field like so:
$this->assertEquals($this->byId('myFirstField')->value(), 1);
$this->assertEquals($this->byId('mySecondField')->value(), 2);
And so on, but as you can se this code wont work, I dont know how to tell phpunit to type something in the current input field. is there a method for calling the element in focus? ie $this->inFocus()->value($i); ?
and btw I can't use $input[$i-1]->value($i); because the tab order and the index of the fields will not be the same.
This is a basic example using java, should be able to use a simulair solution in php I guess.
TabOrderElements.class{
public WebElement element1 = driver.findElement(By.id(""));
public WebElement element2 = driver.findElement(By.id(""));
public WebElement element3 = driver.findElement(By.id(""));
public WebElement element4 = driver.findElement(By.id(""));
public WebElement element5 = driver.findElement(By.id(""));
}
public void validateTabOrder(){
TabOrderElements tabOrderElements = new TabOrderElements();
public String[] tabOrder = {
"element1",
"element2",
"element3",
"element4",
"element5"
};
for (String currentField: tabOrder ){
WebElement expected = (WebElement)elements.getClass().getField(currentField).get(tabOrderElements );
WebElement actual = driver.switchTo().activeElement();
String assertMessage = "Incorrect tab order when trying to tab to: "+fieldName;
Assert.assertEquals(assertMessage, expected, actual);
driver.switchTo().activeElement().sendKeys(Keys.TAB);
}
}