YONGFEIUALL

izheyi.com


  • Home

  • Archives

  • Categories

  • Tags

  • About

  • Search

使用Java操作MongoDB - Delete

Posted on 2014-12-22 | In MongoDB |

Delete

1
2
3
4
5
6
7
8
9
10
Mongo mg = new Mongo("localhost", 27017);
// get db
db = mg.getDB("CRUD");
// get collection
dc = db.getCollection("test");

//Delete
DBObject doc = dc.findOne();
WriteResult result = dc.remove(doc);
System.out.println("Number of documents are deleted : " + result.getN());

1
2
3
// delete
WriteResult wr = dc.remove(new BasicDBObject("name", "tom"));
System.out.println(wr.getN());

使用Java操作MongoDB - Insert

Posted on 2014-12-22 | In MongoDB |

Insert
Can use the below 4 methods to implement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Mongo mg = new Mongo("localhost", 27017);
// get db
db = mg.getDB("CRUD");
// get collection
dc = db.getCollection("test");

//1st save
BasicDBObject doc = new BasicDBObject();
doc.put("name", "add");
doc.put("method", "save");
dc.save(doc);

//2nd insert (using BasicDBObject)
BasicDBObject document = new BasicDBObject();
document.put("database", "mongo");
document.put("table", "testing");
BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put("records", "99");
documentDetail.put("index", "vps_index1");
documentDetail.put("active", "true");
document.put("detail", documentDetail);
dc.insert(document);

//3rd insert (using BasicDBObjectBuilder)
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("database", "mkyongDB")
.add("table", "hosting");
dc.insert(documentBuilder.get());

//3rd insert (using Map)
Map<String, Object> documentMap = new HashMap<String, Object>();
documentMap.put("name", "insert");
documentMap.put("method", "map");
dc.insert(new BasicDBObject(documentMap));

//4th insert (using JSON --> simplest way)
String json = "{name : 'insert', recommend : true, method : 'JSON'}";
DBObject dbObject = (DBObject) JSON.parse(json);
dc.insert(dbObject);

Insert Multiple Documents
For multiple data, we can implement this with previous 4th method:

1
2
3
4
5
6
7
8
String[] myList = {"{number : 1, name: 'hulu1', tags: ['test', 'develop'], comments: [{user: 'hu', like: 0}, {user: 'lu', like: 1}]}"};	
InsertMultipleDocuments(myList);

public static void InsertMultipleDocuments(String[] array) {
for (String json : array) {
DBObject dbObject = (DBObject) JSON.parse(json);
dc.insert(dbObject);
}

使用Java操作MongoDB - Basic Usage

Posted on 2014-12-20 | In MongoDB |

List the basic usage, will introduce ‘CRUD’ in the following blogs:

1
2
3
4
5
6
7
8
9
10
11
12
13
Mongo mg = new Mongo("localhost", 27017);

// get db
db = mg.getDB("CRUD");

// get collection
dc = db.getCollection("test");

//remove collection
db.getCollection("test").drop();

//remove db
db.dropDatabase();

使用Java操作MongoDB

Posted on 2014-12-20 | In MongoDB |
  1. 首先,下载mongoDB对Java支持的驱动包 
        驱动包下载

  2. 下面建立一个JavaProject工程,导入下载下来的驱动包。即可在Java中使用mongoDB.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class MongoDb {
    public static void main(String[] args) throws UnknownHostException {
    MongoClient mg = new MongoClient();
    DB db = mg.getDB("CRUD");

    DBCollection users = db.getCollection("test");
    DBCursor cur = users.find();

    while (cur.hasNext()) {
    System.out.println(cur.next());
    }

    System.out.println(cur.count());
    System.out.println(cur.getCursorId());
    System.out.println(JSON.serialize(cur));
    }
    }

Find Broken Links using QTP without directly opening the links

Posted on 2014-12-09 | In QTP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'This is the main page
Browser("Browser").Page("HomePage").Sync

'Find out all the links in the page using ChildObjects
Set oLink = Description.Create
oLink("micclass").Value = "Link"
Set oAllLinks = Browser("Browser").Page("HomePage").ChildObjects(oLink)

'Find out the count of links
iTotalLinks = oAllLinks.Count

'Loop through all the links to find if the link is broken or not
For i=0 to iTotalLinks - 1

'Find out the url for the link
sURL = oAllLinks(i).GetROProperty("url")

'Create a WinHTTP Request using the link's URL
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHTTP.Open "GET", sURL, False
objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"

'Send the Request to the Server and capture the response
objWinHTTP.Send
iReturnVal = objWinHTTP.Status

'Find out if the Link exists or is broken
If iReturnVal = 200 Then
msgbox "Link - " & sURL & " Exists"
ElseIf iReturnVal = 404 Then
msgbox "Link - " & sURL & " is Broken"
Else
msgbox "Code" - iReturnVal
End If

Set objWinHTTP = Nothing

Next

在Win7下环境搭建MongoDB

Posted on 2014-11-28 | In MongoDB |

做Selenium一直都是用的Excel来管理数据驱动的数据,现在想用MongoDB来管理,所以对MongoDB做一个简单的了解应用:
Include the below items:

  • what is mongodb
  • installation(environment)
  • work with java
  • CRUD
  • maybe more if have time

MongoDB的介绍去官网上直接查看就可以了。MongoDB

Read more »

利用MonteScreenRecorder录制视频

Posted on 2014-11-20 | In Selenium Webdriver |

我们可以用以下方式在Selenium Webdriver中capture video.

基本步骤

  1. 从Monte Screen Recorder,下载“MonteScreenRecorder.jar”
  2. 添加Jar包到你的selenium/webdriver eclipse project
  3. 利用“ScreenRecorder” 类创建一个capture对象方法,脚本如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    //If we didn't name the folder, after execution test script, 
    //video file is generated under “Video” folder of current user folder in Windows machine
    //and “Movies” folder on Mac machine.
    public void startRecording() throws Exception
    {

    GraphicsConfiguration gc = GraphicsEnvironment
    .getLocalGraphicsEnvironment()
    .getDefaultScreenDevice()
    .getDefaultConfiguration();

    this.screenRecorder = new ScreenRecorder(gc,
    new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),
    new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
    CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
    DepthKey, 24, FrameRateKey, Rational.valueOf(15),
    QualityKey, 1.0f,
    KeyFrameIntervalKey, 15 * 60),
    new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black",
    FrameRateKey, Rational.valueOf(30)),
    null);
    this.screenRecorder.start();
    }
  4. 在测试脚本的开始处调用 “screenRecorder.start()” 方法,在结尾调用”screenRecorder.stop()”方法,脚本如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Test
    public void search() throws Exception
    {

    openBrowser();
    startingRecording();

    openURL();
    BaiduSearch yy = new BaiduSearch(driver);
    yy.searchFor("searchTest");

    driver.quit();
    stopRecording();
    }
Read more »

Java操作Sql Server 2008数据库

Posted on 2014-11-18 | In Selenium Webdriver |

在做自动化的过程中,遇到了这种情况,做个记录。

  1. 直接去官网下载即可,Microsoft JDBC Driver for SQL Server
    下载解压文件,得到sqljdbc.jar和sqljdbc4.jar。如果你使用的是jre1.7版本,则忽略sqljdbc.jar(因为它用不了,而且如果和sqljdbc4.jar一起用会出错),只留下sqljdbc4.jar。
  2. 配置Classpath
    网上有很多人说要配置这个路径,我没有配置,但是也通过,这里不记录,如果有问题了,就知道可能是Classpath的问题:)
  3. Eclipse应用
    打开Eclipse,创建新工程,把sqljdbc4.jar包添加到工程中。
  4. 创建脚本测试
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class ExecuteSql { 
    public static void main(String [] args)
    {
    String dbURL="jdbc:你的数据库名 ";
    String userName="填写你的用户名,我的是sa";
    String userPwd="填写你的密码";

    try {
    Class.forName(driverName);
    Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
    System.out.println("连接数据库成功");
    } catch(Exception e) {
    e.printStackTrace();
    System.out.print("连接失败");
    }
    }
    }

How to learn test automation

Posted on 2014-10-23 | In Automation Testing |

Automation is very important in the whole SDLC, especially in the agile, the iteration time for each sprint is very short, it’s impossible to only rely on manual testing.

i attened many seminar or meeting, or discuss with person(they are top notch in software testing), the word most times metoned is “automaton”.

here i want to dscribe my opinions based on UI automaiton.

Read more »

Selenium处理Windows程序(进程)

Posted on 2014-10-20 | In Selenium Webdriver |

Selenium WebDriver java 提供了一个专门的WindowsUtils类去和Windows操作系统交互。

就像我们之前说过有时候跑完脚本后,IEDriverServer.exe进程没杀掉,以及处理的方法。但是有一些未知的情况下,还是会有这样的情况。

对测试环境的设置我们可以做一个更好的处理。在Test运行之前和之后,都去关掉相应的进程,这样的话,就算是跑完脚本后,一些相关进程没关闭,再下次运行的时候,也会重新设置运行环境。

以下的方法:

1
2
WindowsUtils.tryToKillByName("IEDriverServer.exe");
WindowsUtils.tryToKillByName("iexplore.exe");

WindowsUtils会去查询指定的进程名称,并kill掉所有运行的相关进程。这儿有一个小问题,如果当前指定进程不存在,就会抛出一个异常,但是也不会影响脚本的运行,Test会继续运行下一个步骤,只是查看的时候会有异常错误信息,感觉有点怪怪的。

Read more »
1…323334…40
唐胡璐

唐胡璐

i just wanna live while i am alive

393 posts
42 categories
74 tags
RSS
LinkedIn Weibo GitHub E-Mail
Creative Commons
© 2022 唐胡璐
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4