我有两个数组列表。我需要使用断言来比较它们,但是当我打印我的数组列表时,一个是垂直的,由12个对象组成,另一个是水平的,所有12个对象在同一行上。当我比较它们时,我会得到一个例外。其中一个列表包含从网站提取的时间值,另一个是我创建的数组列表,根据当前时间增加2小时。两个列表都应该匹配。但是,我不知道这里的错误是什么。理想情况下,我根据当地时间创建的数组列表应该与我从网站上提取的垂直列表相匹配。感谢任何帮助,谢谢!
以下是完整代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.text.SimpleDateFormat;
import java.util.*;
public class TestScenario extends BasePage {
@Test
public void testOne() throws InterruptedException {
// Navigate to website
navigateToUrl("https://www.darksky.net/");
ArrayList timeNow = new ArrayList();
// Create list of WebElements returned by xpath
List<WebElement> elements = getDriver().findElements(By.xpath("//*[@id=\"timeline\"]/div/div[3]"));
// For loop to print list of elements
for (WebElement s1 : elements) {
timeNow.add(s1.getText().toUpperCase());
}
// Date format to print time in hours and AM/PM
SimpleDateFormat sdf = new SimpleDateFormat("ha");
String time = sdf.format(Calendar.getInstance().getTime());
// Calendar object to return date
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// Array created for expected time values
ArrayList timeExpected = new ArrayList();
timeExpected.add("NOW");
// Do while loop to add 2 hours to current time 12 times (24hrs) which is returned from calendar object
int addHours = 2;
int counter = 0;
do {
cal.add(Calendar.HOUR_OF_DAY, addHours);
counter += 1;
timeExpected.add(sdf.format(cal.getTime()));
} while (counter < 11);
// For loops to print all time expected results added to array from do while loop
System.out.println(timeNow);
System.out.println(timeExpected);
Assert.assertEquals(timeNow, timeExpected);
}
这是输出:
[NOW
5AM
7AM
9AM
11AM
1PM
3PM
5PM
7PM
9PM
11PM
1AM]
[NOW, 5AM, 7AM, 9AM, 11AM, 1PM, 3PM, 5PM, 7PM, 9PM, 11PM, 1AM]
java.lang.AssertionError: lists don't have the same size expected [12] but found [1]
Expected :12
Actual :1
答案 0 :(得分:0)
您的代码
List<WebElement> elements = getDriver().findElements(By.xpath("//*[@id=\"timeline\"]/div/div[3]"));
只返回一个文本值为的
的WebElementNOW
5AM
7AM
9AM
11AM
1PM
3PM
5PM
7PM
9PM
11PM
1AM
由于数组大小不匹配,因此您将收到AssertionError。
答案 1 :(得分:0)
问题在于
using System.Collections;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace MyControls
{
public class FilteredComboBox : ComboBox
{
private string oldFilter = string.Empty;
private string currentFilter = string.Empty;
protected TextBox EditableTextBox => GetTemplateChild("PART_EditableTextBox") as TextBox;
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (newValue != null)
{
var view = CollectionViewSource.GetDefaultView(newValue);
view.Filter += FilterItem;
}
if (oldValue != null)
{
var view = CollectionViewSource.GetDefaultView(oldValue);
if (view != null) view.Filter -= FilterItem;
}
base.OnItemsSourceChanged(oldValue, newValue);
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Tab:
case Key.Enter:
IsDropDownOpen = false;
break;
case Key.Escape:
IsDropDownOpen = false;
SelectedIndex = -1;
Text = currentFilter;
break;
default:
if (e.Key == Key.Down) IsDropDownOpen = true;
base.OnPreviewKeyDown(e);
break;
}
// Cache text
oldFilter = Text;
}
protected override void OnKeyUp(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
case Key.Down:
break;
case Key.Tab:
case Key.Enter:
ClearFilter();
break;
default:
if (Text != oldFilter)
{
var temp = Text;
RefreshFilter(); //RefreshFilter will change Text property
Text = temp;
if (SelectedIndex != -1 && Text != Items[SelectedIndex].ToString())
{
SelectedIndex = -1; //Clear selection. This line will also clear Text property
Text = temp;
}
IsDropDownOpen = true;
EditableTextBox.SelectionStart = int.MaxValue;
}
//automatically select the item when the input text matches it
for (int i = 0; i < Items.Count; i++)
{
if (Text == Items[i].ToString())
SelectedIndex = i;
}
base.OnKeyUp(e);
currentFilter = Text;
break;
}
}
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
ClearFilter();
var temp = SelectedIndex;
SelectedIndex = -1;
Text = string.Empty;
SelectedIndex = temp;
base.OnPreviewLostKeyboardFocus(e);
}
private void RefreshFilter()
{
if (ItemsSource == null) return;
var view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();
}
private void ClearFilter()
{
currentFilter = string.Empty;
RefreshFilter();
}
private bool FilterItem(object value)
{
if (value == null) return false;
if (Text.Length == 0) return true;
return value.ToString().ToLower().Contains(Text.ToLower());
}
}
}
只返回一个元素:
List<WebElement> elements = getDriver().findElements(By.xpath("//*[@id=\"timeline\"]/div/div[3]"));
因此,请尝试更改分离条件。
答案 2 :(得分:0)
其他答案正确指出问题,要解决此问题,请添加以下内容
WebElement element = getDriver().findElement(By.xpath("//*[@id=\"timeline\"]/div/div[3]"));
List<String> timeNow = Arrays.asList(element.getText().toUpperCase().split("\\s+"));
而不是这个
ArrayList timeNow = new ArrayList();
// Create list of WebElements returned by xpath
List<WebElement> elements = getDriver().findElements(By.xpath("//*[@id=\"timeline\"]/div/div[3]"));
// For loop to print list of elements
for (WebElement s1 : elements) {
timeNow.add(s1.getText().toUpperCase());
}