1 package com.atlassian.webdriver.visualcomparison;
2
3 import com.atlassian.annotations.ExperimentalApi;
4 import com.atlassian.selenium.visualcomparison.v2.settings.PagePart;
5 import org.openqa.selenium.By;
6 import org.openqa.selenium.WebDriver;
7 import org.openqa.selenium.WebElement;
8
9 import javax.annotation.Nonnull;
10 import javax.inject.Inject;
11
12 import static com.google.common.base.Preconditions.checkNotNull;
13
14
15
16
17 @ExperimentalApi
18 public final class VisualComparisonSupport
19 {
20 @Inject
21 private WebDriver webDriver;
22
23
24
25 @Nonnull
26 public PagePart asPagePart(@Nonnull By locator)
27 {
28 return new LocatablePagePart(checkNotNull(locator, "locator"));
29 }
30
31 private final class LocatablePagePart implements PagePart
32 {
33 private final By by;
34 private WebElement element;
35
36 private LocatablePagePart(By by)
37 {
38 this.by = by;
39 }
40
41 @Override
42 public int getLeft()
43 {
44 return getElement().getLocation().getX();
45 }
46
47 @Override
48 public int getTop()
49 {
50 return getElement().getLocation().getY();
51 }
52
53 @Override
54 public int getRight()
55 {
56 return getLeft() + getElement().getSize().getWidth();
57 }
58
59 @Override
60 public int getBottom()
61 {
62 return getTop() + getElement().getSize().getHeight();
63 }
64
65 private WebElement getElement()
66 {
67 if (element == null)
68 {
69 element = webDriver.findElement(by);
70 }
71 return element;
72 }
73 }
74 }